I have loaded XmlDocument into memory and created new XmlElement. Now I am trying to add XmlElement to the path /report/section/hosts but I don’t know how. I can add it easily below root node of XML but I cannot figure out how can I navigate deeper level in XML and just append there. In pseudo I am trying to do this:
doc.SelectNodes(‘/report/section/hosts’).AppendChild(subRoot);
The code:
XmlDocument doc = new XmlDocument(); doc.Load('c:\\data.xml'); //host XmlElement subRoot = doc.CreateElement('host'); //Name XmlElement ElName = doc.CreateElement('name'); XmlText TxtName = doc.CreateTextNode('text text'); ElName.AppendChild(TxtName); subRoot.AppendChild(ElName); doc.DocumentElement.AppendChild(subRoot); doc.Save('c:\\data.xml');
Try SelectSingleNode instead of SelectNodes