Manipulating XML documents

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to insert items to an xml file:

1. Search for the right node where the new item is to be added as a child.
2. Add the new item with its attributes.

I am looking at XPathNavigator and XMLReader but cant find the functions to
do the job. Suggestions are welcome. Thanks in advance.
 
Newbie said:
I am trying to insert items to an xml file:

1. Search for the right node where the new item is to be added as a child.
2. Add the new item with its attributes.

I am looking at XPathNavigator and XMLReader but cant find the functions to
do the job.

Those don't help to manipulate XML, if the XML fits into memory then
XmlDocument in System.Xml is a nice way to read in the whole XML
document into a tree structure of nodes which allows creation and
insertion of new nodes and later saving of the whole tree.
 
// create the document
XmlDocument xmlDoc = new XmlDocument();
// load the file
xmlDoc.Load(filename);
// goto the node
XmlNode node = xmlDoc.SelectSingleNode("/path/path/node");
// append new node
XmlNode newNode = node.AppendChild(xmlDoc.CreateElement("Element"));
// create attribute
XmlAttribute at =
newNode.Attributes.Append(xmlDoc.CreateAttribute("test"));
// set value
at.value = "value";
 
Back
Top