Question

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?

Was it helpful?

Solution

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

OTHER TIPS

if (ownerID != null)

Xpath is not correct in last line

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top