Understanding the DOM Implementation

This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".

 
Microsoft.NET supports the W3C DOM Level 1 and Core DOM Level 2 specifications. The .NET Framework provides DOM implementation through many classes. XmlNode and XmlDocument are two of them. By using these two classes, you can easily traverse though XML documents in the same manner you do in a tree.
 
The XmlNode class
 
The XmlNode class is an abstract base class. It represents a tree node in a document. This tree node can be the entire document. This class defines enough methods and properties to represent a document node as a tree node and traverse though it. It also provides methods to insert, replace, and remove document nodes.
 
The ChildNodes property returns all the children nodes of current node. You can treat an entire document as node and use ChildNodes to get all nodes in a document. You can use the FirstChild, LastChild, and HasChildNodes triplet to traverse from a document's first node to the last node. The ParentNode, PreviousSibling, and NextSibling properties return the parent and next sibling node of the current node. Other common properties are Attributes, Base URI, InnerXml, Inner Text, Item Node Type, Name, Value, and so on.
 
You can use the CreateNavigator method of this class to create an Xpath Navigator object, which provides fast navigation using xpath. The Appendchilds, InsertAfter, and InsertBefore methods add nodes to the document. The Remove All, Remove Child, and ReplaceChild methods remove or replace document nodes, respectively. You'll implement these methods and properties in the example after discussing a few more classes.
 
The XML Document Class
 
The XmlDocument class represents an XML document. Before it's derived from the XmlNode class, it supports all tree traversal, insert, remove, and replace functionality. In spite of XmlNode functionaality, this class contains many useful methods.
 
Loading a Document
 
DOM is a cache tree representation of an XML document. The Loads and LoadXml methods of this class load XML data and documents, and the Save method saves a document.
 
The Load Method can load a document from a string, stream, TextReader, or XmlReader. This code example loads the document books.xml from a string:
  1. XmlDocument xmlDoc = new XmlDocument();  
  2. string filename = @"c:\books. Xml";  
  3. xmlDoc.Load(filename);  
  4. xmlDoc.Save(Console.Out); 
This example uses the Load method to load a document from an XmlReader:
  1. XmlDocument xmlDoc = new XmlDocument();  
  2. XmlTextReader reader = new XmlTextReader("c:\\books.xml");  
  3. xmlDoc.Load(reader);  
  4. xmlDoc.Save(Console.Out); 
The LoadXml method loads a document from the specified string. For example
  1. xmlDoc.LoadXml("<Record> write something</ Record>"); 
Saving a Document
 
The Save methods saves a document to a specified location. The Save method takes a paramenter of XmlWriter, XmlTextWriter or string type:
  1. string filename = @"C:\books.xml";  
  2. XmlDocument xmlDoc = new XmlDocument();  
  3. xmlDoc.Load(filename);  
  4.   
  5. XmlTextWriter writer = new XmlTextWriter("c:\\domtest.Xml"null);  
  6. writer.Formatting = Formatting.Indented;  
  7. xmlDoc.Save(writer); 
You can also use a filename or Console.Out to save output as file or on the console:
  1. xmlDoc.Save("c:\\domtest. Xml");  
  2. xmlDoc.Save(Console.Out); 
The XmlDocumentFragment class
 
Usually, you would use this class when you need to insert a small fragment of an XML document or node into a document. This class also comes from XmlNode. Because this class is derived from XmlNode, it has the same tree node traverse, insert, remove, and replace capabilities.
 
You usually create this class instance by calling Xml Document's CreateDocumentFragment method. The InnerXml represents the children of this node. Listing 6-16 shows an example of how to create XmlDocumentFragment and load a small piece of XML data by setting its InnerXml property.
 
Listing 6-16. XmlDocumentFragment sample
  1. //open an XML file  
  2. string filename = @"c:\books.xml";  
  3. XmlDocument xmlDoc = new XmlDocument();  
  4. xmlDoc.Load(filename);  
  5.   
  6. // Create a document fragment.  
  7. XmlDocumentFragment docFrag = xmlDoc.CreateDocumentFragment();  
  8.   
  9. // Set the contents of the document Fragment.  
  10. docFrag.InnerXml = "<Record> write something</ Record>";  
  11.   
  12. // Display the document fragment.  
  13. Console.WriteLine(docFrag.InnerXml); 
You can use XmlNode methods to add, remove, and replace data. Listing 6-17 appends a node in the document fragment.
 
Listing 6-17. Appending in an XML document fragment
  1. XmlDocument doc = new XmlDocument();  
  2. doc.LoadXml("<book genre = "programming"> " +  
  3. "<title> ADO.NET programming </ title> " + "</book>");  
  4.   
  5. // Get the root node  
  6. XmlNode root = doc.DocumentElement;  
  7.   
  8. // Create a new node.  
  9. XmlElement newbook = doc.CreateElement("price");  
  10. newbook.InnerText = "44.95";  
  11.   
  12. // Add the node to the document.  
  13. root.AppendChild(newbook);  
  14. doc.Save(Console.Out); 
The Xml Element Class
 
An XmlElement class object represents an element in a document. This class comes from the XmlLinkedNode class, which comes from XmlNode (see figure 6-8).
 
Figure-6.8.gif
 
Figure 6-8. XML element inheritance
 
The XmlLinkedNode has two useful properties: NextSibing and previousSibling. As their names indicate, these properties return the next and previous nodes of an XML document's current node.
 
The XmlElement class implements and overrides some useful methods for adding and removing attributes and elements (see table 6-7).
 
Table 6-7. Some XML element methods
 
METHOD
DESCRIPTION
GetAttribute
Returns the attribute value
HasAttribute
Checks if a node has the specified attribute
RemoveAll
Removes all the children and attributes of the current node
RemoveAllAttributes, RemoveAttribute
Removes all attributes and specified attributes from an element respectively
RemoveAttributeAt
Removes the attribute node with the specified index from the attribute collection
RemoveAttributeNode
Removes an XmlAttribute
SetAttribute
Sets the value of the specified attribute
SetAttribute Node
Adds a new xml Attribute
 
In the later examples. I'll show you how you can use these methods in your programs to get and set XML element attributes.
 
Adding Nodes to a Document
 
You can use the AppendChild method to add to an existing document. The AppendChild method takes a single parameter of XmlNode type. The XmlDocument's Createxxx methods can create different types of nodes. For example, the CreateComment and CreateElement methods create comment and element node types. Listing 6-18 shows an example of adding two nodes to a document.
 
Listing 6-18. Adding nodes to a document
  1. XmlDocument xmlDoc = new XmlDocument();  
  2. xmlDoc.LoadXml("<Record> some value </Record>");  
  3.   
  4. // Adding a new comment node to the document  
  5. XmlNode node1 = xmlDoc.CreateComment("DOM Testing sample");  
  6. xmlDoc.AppendChild(node1);  
  7.   
  8. // Adding a First Name to the documentt  
  9. node1 = xmlDoc.CreateElement("First Name");  
  10. node1.InnerText = "Mahesh";  
  11. xmlDoc.DocumentElement.AppendChild(node1);  
  12. xmlDoc.Save(Console.Out); 
Getting the Root Node
 
The DocumentElement method of the XmlDocument class (inherited from XmlNode) returns the root node of a document. The following example shows you how to get the root of a document (see listing 6-19).
 
Listing 6-19. Getting the root node of a document
  1. string filename = @"c:\books.xml";  
  2. XmlDocument xmlDoc = new XmlDocument();  
  3. xmlDoc.Load(filename);  
  4. XmlElement root = xmlDoc.DocumentElement; 
Removing and Replacing Nodes
 
The RemoveAll method of the XmlNode class can remove all elements and attributes of a node. The RemoveChild removes the specified child only. The following example calls RemoveAll to remove all elements that had attributed. Listing 6-20 calls RemoveAll to remove all items of a node.
 
Listing 6-20. Removing all item of a node
  1. {  
  2.     static void Main(string[] args)  
  3.     {  
  4.         // Load a document fragment  
  5.         XmlDocument xmlDoc = new XmlDocument();  
  6.         xmlDoc.LoadXml("<book genre ="programming">" +  
  7.         "<title> ADO.NET programming </title> </book>");  
  8.         XmlNode root = xmlDoc.DocumentElement;  
  9.         Console.WriteLine("XML Document Fragment");  
  10.         Console.WriteLine("= = = = = = = = = = = ");  
  11.         xmlDoc.Save(Console.Out);  
  12.         Console.WriteLine();  
  13.         Console.WriteLine("-----------");  
  14.         Console.WriteLine("XML Document Fragment Remove All");  
  15.         Console.WriteLine("= = = = = = = = = = =");  
  16.   
  17.         // Remove all attributes and child nodes.  
  18.         root.RemoveAll();  
  19.   
  20.         // Display the contents on the console after  
  21.         // Removing elements and attributes  
  22.         xmlDoc.Save(Console.Out);  
  23.     }  
Note: You can apply the Remove All method on the books.xml files to delete all the data, but make sure to have backup copy first!
 
Listing 6-21 shows how to delete all the items of books.Xml
 
Listing 6-21.CallingRemoveAll for books.Xml
  1. public static void Main()  
  2. {  
  3.     string filename = "c:\\ books.Xml";  
  4.     XmlDocument xmlDoc = new XmlDocument();  
  5.     xmlDoc.Load(filename);  
  6.     XmlNode root = xmlDoc.DocumentElement;  
  7.     Console.WriteLine("XML Document Fragment");  
  8.     Console.WriteLine("= = = = = = = = = = = ");  
  9.     xmlDoc.Save(Console.Out);  
  10.     Console.WriteLine();  
  11.     Console.WriteLine("- - - - - - - - - ");  
  12.     Console.WriteLine("XML Document Fragment After RemoveAll");  
  13.     Console.WriteLine("= = = = = = = = = = = = ");  
  14.   
  15.     //Remove all attributes and child nodes.  
  16.     root.RemoveAll();  
  17.   
  18.     // Display the contents on the console after  
  19.     // Removing elements and attributes  
  20.     xmlDoc.Save(Console.Out);  
The ReplaceChild method replaces an old child with a new child node. In Listing 6-22, ReplaceChild replaces root Node; Last Child with xmlDocFrag.
 
Listing 6-22 Replace Child method sample
  1. string filename = @"C:\books.xml";  
  2. XmlDocument xmlDoc = new XmlDocument();  
  3. xmlDoc.Load(filename);  
  4. XmlElement root = xmlDoc.DocumentElement;  
  5. XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();  
  6. xmlDocFragment.InnerXml =  
  7. "<Fragment><SomeData>Fragment Data</SomeData></ Fragment>";  
  8. XmlElement rootNode = xmlDoc.DocumentElement;  
  9.   
  10. //Replace xmlDocFragment with rootNode.LastChild  
  11. rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild);  
  12. xmlDoc.Save(Console.Out); 
Inserting XML Fragments into an XML Document
 
As discussed previously, the XmlNode class is useful for navigating through the nodes of a document. It also provides other methods to insert XML fragments into a document. For instance, the InsertAfter method inserts a document or element after the current node. This method takes two arguments. A first argument is an XmlDocumentFragment object, and the second argument is the position of where you want to insert the fragment. As discussed earlier in this article, you create an XmlDocumentFragment class object by using the CreateDocumentFragment method of the XmlDocument class. Listing 6-23 inserts an XML fragment into a document after the current node using InsertAfter.
 
Listing 6-23. Inserting an XML fragment into a document
  1. XmlDocument xmlDoc = new XmlDocument();  
  2. xmlDoc.Load(@"C:\\ books.Xml");  
  3. XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();  
  4. xmlDocFragment.InnerXml =  
  5. "< Fragment >< Some Data> Fragment Data</ Some Data> </ Fragment>";  
  6. XmlNode aNode = xmlDoc.DocumentElement.FirstChild;  
  7. aNode.InsertAfter(xmlDocFragment, aNode.LastChild);  
  8. xmlDoc.Save(Console.Out); 
Adding Attributes to a Node
 
You use the SetAttributeNode method of xmlElement to add attributes to an element, which is a Node. The XmlAttribute represents an XML attribute. You create an instance of XmlAttribute by calling CreateAttribute of XmlDocument. After that, you call an XML Element's Set Attribute method to set the attribute of an element. Finally, you append this new item to the document (see listing 6-24).
 
Listing 6-24. Adding a node with attributes
  1. XmlDocument xmlDoc = new XmlDocument();  
  2. xmlDoc.Load(@"c:\\books.Xml");  
  3. XmlElement newElem = xmlDoc.CreateElement("NewElement");  
  4. XmlAttribute newAttr = xmlDoc.CreateAttribute("NewAttribute");  
  5. newElem.SetAttributeNode(newAttr);  
  6.   
  7. // add the new element to the document  
  8. XmlElement root = xmlDoc.DocumentElement;  
  9. root.AppendChild(newElem);  
  10. xmlDoc.Save(Console.Out); 

Conclusion

 
Hope this article would have helped you in Understanding DOM Implementation. See other articles on the website also for further reference.
adobook.jpg This essential guide to Microsoft's ADO.NET overviews C# then leads you toward a deeper understanding of ADO.NET.