2012-11-12 60 views
1

我試圖更改具有子節點的XML節點的現有屬性值。試圖這樣做時,我收到了一個異常。更改XML節點的屬性值

string specificOwnerPath = "Owners/Owner[@id='" + startingOwnerName + startingOwnerZipCode + "']"; 
XmlNode ownerID = doc.SelectSingleNode(specificOwnerPath); 
ownerID.Attributes["id"].Value = ownerNameTextBox.Text + ownerZipCodeTextBox.Text; 

我得到的異常說'對象引用未設置爲對象的實例,並且發生在上面顯示的代碼的最後一行。有沒有人對我做錯了什麼有什麼建議?

+0

幾乎所有NullReferenceException的情況都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 –

回答

2

的最後一行之前添加復

if (ownerID != null) 

來。很可能你的XPath不正確(或者根本沒有任何匹配的元素)。