I'm getting this error when trying to append a node to existing element in my xml document.The error is: Object reference not set to an instance of an object.
<houses>
<house windowsc="three">
<wind>0</wind>
<windows>
</windows>
</house>
</houses>
Code:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("C:\\Houseplans.xml");
XmlElement xhousing = xDoc.DocumentElement["houses/house[@windowsc=\"three\"]/windows"];
XmlNode xName = xDoc.CreateElement("Name");
xName.InnerText = "hi";
xhousing.AppendChild(xName);
You want to use SelectSingleNode:
XmlNode xhousing = xDoc.SelectSingleNode(@"//house[@windowsc='three']/windows");
XmlNode xName = xDoc.CreateElement("Name");
xName.InnerText = "hi";
xhousing.AppendChild(xName);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.