문제

I am trying to change an existing attribute value of an XML Node that has child nodes. I am getting an exception when trying to do this.

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

The exception I am getting says 'Object reference not set to the instance of an object and is occurring at the final line of code shown above. Does anyone have any suggestions on what I am doing wrong?

도움이 되었습니까?

해결책

Add a check

if (ownerID != null)

to before the last line. Most probably your XPath is not correct (or there simply isn't any matching element).

다른 팁

if (ownerID != null)

Xpath is not correct in last line

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top