1

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.

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35

1 Answers1

0

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));
         }
     }
}
Chaz
  • 319
  • 1
  • 6
  • Hey thanks for the help, i tried to test it your way but i keep getting the error "NullReferenceException was unhandled by user code" any ideas what is that about? Cheers. – F. Schneider Nov 23 '15 at 15:53
  • This was really meant as an example, not complete code, but it should work, at least against the xml file provided at the top. If your actual XML file is different, you may need to adjust the code, in particular, I suspect the null reference is on one of the m_textElem* not being set because the SelectSingleNode function did not find the element in the xml. Another source of that error can result from your controls not being named textBox1, textBox2, etc. – Chaz Nov 23 '15 at 16:06
  • Yes i have adjusted to my code and checked spelling from both form and xml file. My xml file is the same as you wrote it but with more nodes and its linked to an xml schema. I still get the same null reference error, I'm sure of the form controls named as stated and almost sure i wrote the correct path for the SelectSingleNode, can you kindly tell me what can be done to fix this error in particular? Cheers – F. Schneider Nov 23 '15 at 16:27
  • Is the error when you run the program or in the designer? – Chaz Nov 23 '15 at 17:53
  • When i run the designer, as i placed your code from public string TextElement1Content until before the public form1 (), under my Form1_Load method. so when i run my designer it gives me the error in this piece of code specifically. – F. Schneider Nov 24 '15 at 00:02