5.2 Insert Nodes in an XML Document


Problem

You need to modify an XML document by inserting new data, or you want to create an entirely new XML document in memory.

Solution

Create the node using the appropriate XmlDocument method (such as CreateElement , CreateAttribute , CreateNode , and so on). Then insert it using the appropriate XmlNode method (such as InsertAfter , InsertBefore , or AppendChild ).

Discussion

Inserting a node into the XmlDocument class is a two-step process. You must first create the node, and then you insert it at the appropriate location. Optionally, you can then call XmlDocument.Save to persist changes.

To create a node, you use one of the XmlDocument methods that start with the word Create , depending on the type of node. This ensures that the node will have the same namespace as the rest of the document. (Alternatively, you can supply a namespace as an additional string argument.) Next you must find a suitable related node and use one of its insertion methods to add the new node to the tree.

The following example demonstrates this technique by programmatically creating a new XML document.

 using System; using System.Xml; public class GenerateXml {     private static void Main() {         // Create a new, empty document.         XmlDocument doc = new XmlDocument();         XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);         doc.AppendChild(docNode);         // Create and insert a new element.         XmlNode productsNode = doc.CreateElement("products");         doc.AppendChild(productsNode);         // Create a nested element (with an attribute).         XmlNode productNode = doc.CreateElement("product");         XmlAttribute productAttribute = doc.CreateAttribute("id");         productAttribute.Value = "1001";         productNode.Attributes.Append(productAttribute);         productsNode.AppendChild(productNode);         // Create and add the sub-elements for this product node         // (with contained text data).         XmlNode nameNode = doc.CreateElement("productName");         nameNode.AppendChild(doc.CreateTextNode("Gourmet Coffee"));         productNode.AppendChild(nameNode);         XmlNode priceNode = doc.CreateElement("productPrice");         priceNode.AppendChild(doc.CreateTextNode("0.99"));         productNode.AppendChild(priceNode);         // Create and add another product node.         productNode = doc.CreateElement("product");         productAttribute = doc.CreateAttribute("id");         productAttribute.Value = "1002";         productNode.Attributes.Append(productAttribute);         productsNode.AppendChild(productNode);         nameNode = doc.CreateElement("productName");         nameNode.AppendChild(doc.CreateTextNode("Blue China Tea Pot"));         productNode.AppendChild(nameNode);         priceNode = doc.CreateElement("productPrice");         priceNode.AppendChild(doc.CreateTextNode("102.99"));         productNode.AppendChild(priceNode);         // Save the document (to the Console window rather than a file).         doc.Save(Console.Out);         Console.ReadLine();     } } 

The generated document looks like this:

 <?xml version="1.0" encoding="UTF-8"?> <products>   <product id="1001">     <productName>Gourmet Coffee</productName>     <productPrice>0.99</productPrice>   </product>   <product id="1002">     <productName>Blue China Tea Pot</productName>     <productPrice>102.99</productPrice>   </product> </products> 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net