2015-12-03 121 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

相關問題