我目前有一個Windows窗體應用程序,其中包含由用戶更新的列表視圖。當列表視圖更新時,它還會爲項目填充和創建XML,並將其存儲在隱藏文本框中,直到發送保存請求。NullReferenceException當嘗試更新XML C#
發送保存請求時,我調用一個寫入我的外部配置文件以更新指定屬性的函數。
總共我更新7個設置,但失敗,一個NullReferenceException只有一個是,當我嘗試更新列表視圖和保存XML
//The values I'm passing in are the path to my config file, the setting I am updating (in this case would be RequiredDocuments), and the string value to update.
public static void UpdateConfigFiles(string p_sPath, string p_sSettingName, string p_sValue)
{
bool blnApplyChanges = false;
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(p_sPath);
int iCurrentNode = 0;
for (iCurrentNode = 0; iCurrentNode <= xmlDoc.ChildNodes[1]["applicationSettings"].ChildNodes[0].ChildNodes.Count - 1; iCurrentNode++)
if (xmlDoc.ChildNodes[1]["applicationSettings"].ChildNodes[0].ChildNodes[iCurrentNode].Attributes[0].Value.ToString().ToUpper() == p_sSettingName.ToUpper())
{
//This line is where the exception occurs
xmlDoc.ChildNodes[1]["applicationSettings"].ChildNodes[0].ChildNodes[iCurrentNode].ChildNodes[0].InnerText = p_sValue;
blnApplyChanges = true;
}
if (blnApplyChanges)
{
xmlDoc.Save(p_sPath);
FixBlankXMLValues(p_sPath);
}
}
我又增加了手錶,以查看值在所有不同的節點中,還沒有遇到空值,所以我想知道這是否與我構建XML的方式有關。起初,我曾認爲我重新綁定ListView時已清除傳入的值,但事實證明情況並非如此。
Here是帶有一些編輯信息的XML的示例結構。
閱讀愚蠢的答案,並不再困惑。 – Will
@我會理解爲什麼會發生這種情況,以及NullReferenceException〜是什麼〜但是這個問題是針對XML的,並且將這個問題標記爲重複的並不是很有幫助。我以前也曾引用過這個答案 - 感謝你發佈它。 – confusedandamused
你的問題中沒有什麼表示1)你已經調試了這個2)你找到了哪些代碼正在拋出一個NRE和3)爲什麼你不能修復這個異常。重複的問題引導你通過1和2,你真的需要做的事情之前,要求幫助3.如果你可以[編輯]包括1,2和3,請做,並且@我和我很樂意重新打開。 – Will