2015-12-03 65 views
-1

Im新的Winforms和C#。我的表单假设读取一个XML并将其一些节点的值添加到表单文本框中。此外,它应该更新/修改这些节点值由用户插入到文本框中的值,然后保存它们。这是形式的样子:
Form DesignC#:为什么我不断收到我的代码中的System.NullReferenceException

形式代码:
裸记button4_Click =“添加按钮”。 button2_Click ='修改按钮'。

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) 
    { 

    } 
} 

}

从 '添加按钮' 的异常来自:的XmlElement根= xDoc.DocumentElement;

,并从“修改按钮”例外来自:

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

我怎样才能解决这个代码来解决我原来的任务是什么?请帮忙。谢谢

+2

'button2_Click'之后的文本元素包含任何东西?你是否已经通过调试器了解了这一点? –

+3

您首先访问文档,然后才将数据加载到文档中。自然会造成错误。此外,更好地命名您的控件,因此不需要解释任何人button4是这个,button2就是这个。 –

+1

也可以让其他人不必通过你的代码搜索下面的'XmlDocument xDoc = null;'为什么这不是在你的类的顶部声明了其他对象的声明..?那么你有这个声明再次在顶部'XmlDocument doc = new XmlDocument();' – MethodMan

回答

0

您必须在使用它之前初始化一个对象,具体取决于您何时需要使用它。无论是添加某种逻辑的是这样的:

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

或者在构造函数中:

public Form1() { 
    Initialize(); 
    // initialize the object here 
} 
+0

我做了初始化构造函数,它仍然抛出异常。 –

0
XDOC

声明为您button4_Click方法前行空。您无法访问空对象的属性。

我想你需要在访问xDoc.DocumentElement属性之前移动xDoc.Load(ofd.FileName)行。

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

另外,你确定你的xDoc变量的范围?也许它需要纳入你的方法。

+0

感谢您的答案,它的工作原理没有再抛出异常,但它的按钮功能不工作,因为XML文件中的节点不会添加到文本框中。任何想法为什么? –

+0

在button4_CLick函数中,您继续分配给node1,而不是分配给node2和node3。如果我正确理解这个函数,那么你正在从UI文本框中获取项目并更新XML,然后保存它,对吗?如果是这种情况,XML不会更新文本框,这是相反的方式。 – Dave

相关问题