Exploring the XML Document Object Model

The W3C Document Object Model (DOM) is a set of specifications to represent an XML document in the computer's memory. Microsoft has implemented the W3C Document Object Model via a number of .NET objects.The XmlDocument is one of these objects.When an XmlDocument object is loaded, it organizes the contents of an XML document as a "tree" (as shown in Figure 8.18). Whereas the XMLTextReader object provides a forward-only cursor, the XmlDocument object provides fast and direct access to a node. However, a DOM tree is cache intensive, especially for large XML documents.

An XmlDocument object can be loaded from an XmlTextReader. Once it is loaded, we may navigate via the nodes of its tree using numerous methods and properties. Some of the frequently used members are the following: DocumentElement (root of the tree), ChildNodes (all children of a node), FirstChild, LastChild, HasChildNodes, ChildNodes.Count (# of children), InnerText (the content of the sub-tree in text format), Name (node name), NodeType, and Value (of a text node) among many others.

If needed, we may address a node using the parent-child hierarchy.The first child of a node is the ChildNode(O), the second child is ChildNode(1), and so on. For example, the first product can be referenced as DocumentElement .ChildNodes(0). Similarly, the price of the second product can be addressed as DocumentElement.ChildNodes(1).ChildNodes(2).InnerText.

Figure 8.18 Node Addressing Techniques in an XML DOM Tree

Figure 8.18 Node Addressing Techniques in an XML DOM Tree

0 0

Post a comment