windows form, how to edit each node value in xml file by the the value in the textbox in the form. c#

1.4k Views Asked by At

I am new to C# and I'm creating a windows form application that has 2 TextBox controls and 1 RichTextBox.

I want it enable me to open an XML file and insert the values of a node inside the TextBox controls but most importantly I want to insert data in these boxes and update the value inside a node in an external XML file and save it.

This is what I have in terms of the form code.

namespace WindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button4.Enabled = true;
        }

        XmlDocument xDoc;
        string path;
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            xDoc.Save(path);
        }


        private void button2_Click(object sender, EventArgs e)

        {

           xDoc.SelectSingleNode("TwitterCards/Card1/title").InnerText = textBox1.Text;
           xDoc.SelectSingleNode("TwitterCards/Card1/image").InnerText = textBox2.Text;
           xDoc.SelectSingleNode("TwitterCards/Card1/description").InnerText = richTextBox1.Text; xDoc.Save(path);



        }

        XmlDocument xDoc1;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)

            {
                path = ofd.FileName;
                xDoc = new XmlDocument();
                xDoc.Load(path);
                XmlNodeList nodeList = xDoc.DocumentElement.SelectNodes("TwitterCards/Card1");
                string title = "", image = "", description = "";
                foreach (XmlNode node in nodeList)
                {
                    title = node.SelectSingleNode("title").InnerText;
                    image = node.SelectSingleNode("image").InnerText;
                    description = node.SelectSingleNode("description").InnerText;
                    textBox1.Text = (title);
                    textBox2.Text = (image);
                    richTextBox1.Text = (description);

              //  }
                 textBox1.Text = xDoc.SelectSingleNode("TwitterCards/Card1/title").InnerText;
                 textBox2.Text = xDoc.SelectSingleNode("TwitterCards/Card1/image").InnerText;
                 richTextBox1.Text = xDoc.SelectSingleNode("TwitterCards/Card1/description").InnerText;
                }

            }
        }
    }
}

I'm trying to create a form to edit properties of twitter cards <meta> tags. The problem so far is that it doesn't update the value in the XML file with what I inserted in the text box.

1

There are 1 best solutions below

5
On

I don't quite understand your foreach loop, but binding the XmlElements you want to change to properties which update to/from the form controls is how I would approach it:

test.xml:
<?xml version="1.0" encoding="utf-8"?>
<TwitterCards>
  <Card1>
    <Site> @_Paul</Site>
    <title> Schneider's </title>
    <image> "C:\\AAA.jpg" </image>
    <description>foo</description>
  </Card1>
</TwitterCards>

public partial class Form1 : Form, INotifyPropertyChanged
{
    XmlDocument doc = new XmlDocument();
    XmlElement m_textElem1;
    XmlElement m_textElem2;
    XmlElement m_textElem3;

    public string TextElement1Content
    {
        get { return m_textElem1.InnerText; }
        set { m_textElem1.InnerText = value;  }
    }

    public string TextElement2Content
    {
        get { return m_textElem2.InnerText; }
        set { m_textElem2.InnerText = value; }
    }

    public string TextElement3Content
    {
        get { return m_textElem3.InnerText; }
        set { m_textElem3.InnerText = value; }
    }


    public Form1()
    {
        InitializeComponent();

        doc.Load("..\\..\\test.xml");
        m_textElem1 = doc.SelectSingleNode("TwitterCards/Card1/title") as XmlElement;
        m_textElem2 = doc.SelectSingleNode("TwitterCards/Card1/image") as XmlElement;
        m_textElem3 = doc.SelectSingleNode("TwitterCards/Card1/description") as XmlElement;

        textBox1.DataBindings.Add("Text", this, "TextElement1Content");
        textBox2.DataBindings.Add("Text", this, "TextElement2Content");
        richTextBox1.DataBindings.Add("Text", this, "TextElement3Content");
    }

     private void saveButton_Click(object sender, EventArgs e)
     {
         doc.Save("..\\..\\saved.xml");
     }

     public event PropertyChangedEventHandler PropertyChanged;

     public void NotifyPropertyChanged(string propName)
     {
         if (PropertyChanged != null )
         {
             PropertyChanged(this, new PropertyChangedEventArgs(propName));
         }
     }
}