-1

Im new to Winforms and C#. My form is suppose to read an XML and Add some of its nodes' values to the forms textBoxes. Also it should update/modify these nodes values by the values inserted by the user into the text boxes then save them. This is what the form looks like :
Form Design

The form code:
bare in mind button4_Click = 'Add button'. button2_Click = 'Modify Button'.

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace WindowsForm
{
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();
    }


    private void Form1_Load(object sender, EventArgs e)
    {

    }


    private void button2_Click(object sender, EventArgs e)

    {
        doc.Load("C:\\Users\\Fahad\\Documents\\Visual studio 2015\\Projects\\ClassLibrary1\\XMLFile3.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 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)
    {

    }

    XmlDocument xDoc = null;

    private void button4_Click(object sender, EventArgs e)
    {
          OpenFileDialog ofd = new OpenFileDialog();
          if (ofd.ShowDialog() == DialogResult.OK)
           {

            XmlElement root = xDoc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("Twittercards/Card1");
            xDoc.Load(ofd.FileName);

            foreach (XmlNode node in nodes)
            {
                var node1 = xDoc.SelectSingleNode(@"Twittercards/Card1/title");
                node1.InnerText = textBox1.Text;
                var node2 = xDoc.SelectSingleNode(@"Twittercards/Card1/image");
                node1.InnerText = textBox2.Text;
                var node3 = xDoc.SelectSingleNode(@"Twittercards/Card1/description");
                node1.InnerText = richTextBox1.Text;
                xDoc.Save(ofd.FileName);
                // textBox1.Text = xDoc.SelectSingleNode(@"Twittercards/Card1/title").InnerText;
                //  textBox2.Text = xDoc.SelectSingleNode(@"Twittercards/Card1/image").InnerText;
                //  richTextBox1.Text = xDoc.SelectSingleNode(@"Twittercards/Card1/description").InnerText;
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        doc.Save("C:\\Users\\Fahad\\Documents\\Visual studio 2015\\Projects\\ClassLibrary1\\XMLFile3.xml");
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}

}

The exception from the 'Add button' comes from: XmlElement root = xDoc.DocumentElement;

And the exception from the 'Modify button' comes from:

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

How can i fix this code to solve my original task? Please help. Thanks

Glorin Oakenfoot
  • 2,105
  • 15
  • 19
  • 2
    After `button2_Click` do the text elements contain anything? Have you stepped through this with the debugger? – Ron Beyer Dec 03 '15 at 19:15
  • 3
    You are first accessing the document and only then loading data into it. Naturally it will cause an error. Also, name your controls better so there will be no need to explain anyone that button4 is this and button2 is that. – Sami Kuhmonen Dec 03 '15 at 19:17
  • 1
    also so that others do not have to search through your code for the following `XmlDocument xDoc = null;` why is this not declared in the top of your class where you have the rest of your objects declared..? then you have this declared also again at the top ` XmlDocument doc = new XmlDocument();` – MethodMan Dec 03 '15 at 19:21

2 Answers2

0

You have to initialize an object before you use it, depending on when you need to use it. Either add some sort of a logic like this:

if (m_textElem1==null) {
    // initialize the object
}

Or in the constructor:

public Form1() {
    Initialize();
   // initialize the object here
}
GregoryHouseMD
  • 1,726
  • 1
  • 13
  • 34
0

xDoc is declared as null in the line before your button4_Click method. You can't access a property of a null object.

I think you need to move the xDoc.Load(ofd.FileName) line before you access the xDoc.DocumentElement property.

xDoc.Load(ofd.FileName);    
XmlElement root = xDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("Twittercards/Card1");

Also, are you sure about the scope of your xDoc variable? Perhaps it needs to be brought inside your method.

Dave
  • 478
  • 2
  • 12
  • Thanks for your Answer, it worked it didnt throw the exception anymore but it the function of the button doesnt work as the nodes from the XML file are not added to the textBoxes. any ideas why? – T. Davidson Dec 04 '15 at 10:43
  • In the button4_CLick function you keep assigning to node1 instead of assigning to node2 and node3. If i understand the function correctly, you are taking items from the UI textboxes and updating the XML and then saving that, correct? If that is the case the XML wouldn't update the textBoxes, it is the other way around. – Dave Dec 04 '15 at 15:02