DOM Implementation in VB.NET

In this article I will explain you about DOM implementation in C# and .NET.
  • 4691

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:

        Dim xmlDoc As New XmlDocument()
        Dim filename As String = "c:\books. Xml"

This example uses the Load method to load a document from an XmlReader:

        Dim xmlDoc As New XmlDocument()
        Dim reader As New XmlTextReader("c:\books.xml")

The LoadXml method loads a document from the specified string. For example

        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:

        Dim filename As String = "C:\books.xml"
        Dim xmlDoc As New XmlDocument()

        Dim writer As New XmlTextWriter("c:\domtest.Xml", Nothing)
        writer.Formatting = Formatting.Indented

You can also use a filename or Console.Out to save output as file or on the console:

        xmlDoc.Save("c:\domtest. Xml")

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

        'open an XML file
        Dim filename As String = "c:\books.xml"
        Dim xmlDoc As New XmlDocument()

        ' Create a document fragment.
        Dim docFrag As XmlDocumentFragment = xmlDoc.CreateDocumentFragment()

        ' Set the contents of the document Fragment.
        docFrag.InnerXml = "<Record> write something</ Record>"

        ' Display the document fragment.

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

        Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml("<book genre = "programming"> " + "<title> ADO.NET programming </ title> " + "</book>")

        ' Get the root node
        Dim root As XmlNode = doc.DocumentElement

        ' Create a new node.
        Dim Newbook As XmlElement = doc.CreateElement("price")
        Newbook.InnerText = "44.95"

        ' Add the node to the document.

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. 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 element (see table 6-7).

Table 6-7. Some xml element methods





Returns the attribute value


Checks if a node has the specified attribute


Removes all the children and attributes of the current node

RemoveAllAttributes, RemoveAttribute

Removes all attributes and specified attributes from an element respectively


Removes the attribute node with the specified index from the attribute collection


Removes an XmlAttribute


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

        Dim xmlDoc As New XmlDocument()
        xmlDoc.LoadXml("<Record> some value </Record>")

        ' Adding a new comment node to the document
        Dim node1 As XmlNode = xmlDoc.CreateComment("DOM Testing sample")

        ' Adding a First Name to the documentt
        node1 = xmlDoc.CreateElement("First Name")
        node1.InnerText = "Mahesh"

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 root node of a document

        Dim filename As String = "c:\books.xml"
        Dim xmlDoc As New XmlDocument()
        Dim root As XmlElement = 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 had attributes. Listing 6-20 calls RemoveAll to remove all item of a node.

Listing 6-20. Removing all item of a node

    Shared Sub Main(ByVal args() As String)
        ' Load a document fragment
        Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.LoadXml("<book genre ="programming">" +
"<title> ADO.NET programming </title> </book>")
        Dim root As XmlNode = xmlDoc.DocumentElement
        Console.WriteLine("XML Document Fragment")
        Console.WriteLine("= = = = = = = = = = = ")
        Console.WriteLine("XML Document Fragment Remove All")
        Console.WriteLine("= = = = = = = = = = =")

        ' Remove all attribute and child nodes.

        ' Display the contents on the console after
        ' Removing elements and attributes
    End Sub

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 item of books. Xml

Listing 6-21.CallingRemoveAll for books.Xml

    Public Shared Sub Main()
        Dim filename As String = "c:\ books.Xml"
        Dim xmlDoc As New XmlDocument()
        Dim root As XmlNode = xmlDoc.DocumentElement
        Console.WriteLine("XML Document Fragment")
        Console.WriteLine("= = = = = = = = = = = ")
        Console.WriteLine("- - - - - - - - - ")
        Console.WriteLine("XML Document Fragment After RemoveAll")
        Console.WriteLine("= = = = = = = = = = = = ")

        'Remove all attribute and child nodes.

        ' Display the contents on the console after
        ' Removing elements and attributes
    End Sub

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

        Dim filename As String = "C:\books.xml"
        Dim xmlDoc As New XmlDocument()
        Dim root As XmlElement = xmlDoc.DocumentElement
        Dim xmlDocFragment As XmlDocumentFragment = xmlDoc.CreateDocumentFragment()
        xmlDocFragment.InnerXml = "<Fragment><SomeData>Fragment Data</SomeData></ Fragment>"
        Dim rootNode As XmlElement = xmlDoc.DocumentElement

        'Replace xmlDocFragment with rootNode.LastChild
        rootNode.ReplaceChild(xmlDocFragment, rootNode.LastChild)

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. The 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

        Dim xmlDoc As New XmlDocument()
        xmlDoc.Load("C:\\ books.Xml")
        Dim xmlDocFragment As XmlDocumentFragment = xmlDoc.CreateDocumentFragment()
        xmlDocFragment.InnerXml = "< Fragment >< Some Data> Fragment Data</ Some Data> </ Fragment>"
        Dim aNode As XmlNode = xmlDoc.DocumentElement.FirstChild
        aNode.InsertAfter(xmlDocFragment, aNode.LastChild)

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

        Dim xmlDoc As New XmlDocument()
        Dim newElem As XmlElement = xmlDoc.CreateElement("NewElement")
        Dim newAttr As XmlAttribute = xmlDoc.CreateAttribute("NewAttribute")

        ' add the new element to the document
        Dim root As XmlElement = xmlDoc.DocumentElement


Hope this article would have helped you in Understanding the DOM Implementation. See other articles on the website also for further reference.


More Articles

© 2020 DotNetHeaven. All rights reserved.