XML Document Object Model
The .NET framework offers classes that can read and manipulate XML documents. These classes are located in the System.Xmlnamespace. .NET uses the XML Document Object Model which is a set of classes that represents different parts of an XML document (and the document itself). The following are some of the classes offered by XML DOM.
|XmlNode||Represents a node in the XML document tree. Nodes are anything you see in an xml file such as elements attributes, comments, and even white spaces. This class is the base class of the other XML DOM classes.|
|XmlDocument||Represents the actual XML document. Can be used to load an existing XML file or save a constructed XML file.|
|XmlElement||Represents a single XML element.|
|XmlAttribute||Represents a single XML attribute.|
|XmlText||Represents that text inside an XML element.|
|XmlComment||Represents an XML comment.|
Figure 1 – XML DOM Classes
An XML Document is represented by the XmlDocument class. It contains the actual XML markup that you will be reading or manipulating. An XML Document should start with a root element which is also called the document element. This can be represented as an XmlElement. Everything inside it is its child nodes. You must also understand the concept of parent and child nodes. A parent node is a node that contains child nodes. As an example, consider this element that contains multiple elements:
<Product> <ProductID>001</ProductID> <ProductName>Shampoo</ProductName> <Price>10</Price> </Product>
Here, the parent node is the Product element. It contains two child nodes which are the ProductID, ProductName, and Price elements. If for example the ProductName still has more elements inside it, then it will be considered as a parent node of those elements it contains.
Let’s take a look at some properties and methods of each of these classes.
The XmlNode represents any part of the XML and is the base class of other classes in the XML DOM. For example, the XmlElement class is derived from the XmlNode so it shares XmlNode’s properties and methods. The following are some useful properties of the XmlNodebase class.
|Attributes||A collection of XmlAttribute objects which represents the attributes that this node contain.|
|ChildNodes||A collection of XmlNodes that serves as child nodes of this node.|
|FirstChild||Gets the first child node of this node.|
|HasChildNodes||Tells whether this node has child nodes.|
|InnerText||Gets all the inner text of all the child nodes of these nodes. If multiple inner texts are present, then they are combined into a single string.|
|InnerXml||Gets the XML string between the opening and closing tags of this node.|
|LastChild||Get the last child node of this node.|
|Name||Gets the name of the node.|
|NextSibling||Get the node immediately following this node.|
|NodeType||Gets the type of this node.|
|OuterXml||Gets the markup representing this node and all its child nodes.|
|OwnerDocument||Gets the XmlDocument to which this node belongs.|
|ParentNode||Gets the parent node of this node.|
|PreviousSibling||Get the node immediately preceding this node.|
|Value||Gets or sets the value of this node.|
Figure 2 – XmlNode Properties
The Attributes property of a node contains a list of attributes that a node or element contains. Each attribute is represented by the XmlAttribute class. Consider this example:
<Person name="John Smith" age="30"></Person>
This element’s Attributes property contains two XmlAttribute objects that hold data for the name and age attribute.
The ChildNodes property is a collection of child nodes of the current node. Each child node is also an XmlNode object. You can test if the node has child nodes by checking the value of the HasChildNodes property. This will return true if it has at least one or more child nodes, and false if it has none. The FirstChild and LastChild properties simply get the first and last child nodes of the current node. PreviousSibling and NextSibling properties gets the previous and next nodes of the current node.
<PreviousNode></PreviousNode> <CurrentNode></CurrentNode> <NextNode></NextNode>
The Parent node gets the reference the parent of a child node. If no parent exists, then this property contains null.
The Name property indicates the name of the element.
The Name of the above element is Person.
The InnerText retrieves the text located between the opening and closing tags of a control. If you are working with a comment, then this property returns the text of the comment. If the element has multiple nodes inside it, the InnerText property gets all the inner texts of all the child nodes and combine them into a single string. Consider the following XML markup.
<Example> <Sample1>Text1</Sample1> <Sample2>Text2</Sample2> <Sample3> <Sample4>Text3</Sample4> </Sample3> </Example>
The InnerText property will produce the output Text1Text2Text3. Notice that the third child element has another child node and the InnerText property still successfully retrieve the text inside that child node.
The InnerXml is similar to InnerText property, but this property returns the actual XML markup inside an element. The OuterXmlproperty is similar to InnerXml but includes the current node in the results.
The OwnerDocument property returns the reference to an XmlDocument that owns the current node.
The NodeType contains a value from System.Xml.XmlNodeType enumeration. You can tell the type of a node by using this property. The table below lists the values of the XmlNodeType enumeration.
|Element||The node is an element.|
|Attribute||The node is an attribute.|
|Text||The node is a text.|
|Comment||The node is a comment.|
|Document||The root element of a document.|
|Whitespace||A whitespace such as space, new lines or tab.|
|XmlDeclaration||An XML Declaration element.|
Figure 3 – XmlNodeType Enumeration Values
Finally, the Value property specifies the value of the node. The Value property varies on each type of node. For example, calling the Value property a comment returns the text of the comment. The Value property of an attribute is the value of the attribute. Note that you must use InnerText or InnerXml to access contents of an XmlElement instead of using the Value property.
The following are some useful methods of the XmlNode class.
|AppendChild||Adds a new child node for this node.|
|InsertAfter||Insert a node immediately after the end of this node.|
|InsertBefore||Insert a node immediately before the beginning of this node.|
|PrependChild||Adds a node to the beginning of the list of the child nodes of this node.|
|RemoveAll||Removes the child nodes and attributes of this node.|
|RemoveChild||Removes a specified child node.|
|ReplaceChild||Replaces an old child node with a new one.|
|SelectNodes||Selects a list of node matching an XPath expression.|
|SelectSingleNode||Selects the first node that matches the XPath expression.|
|WriteContentTo||Writes all the child nodes of a node to an XmlWriter.|
|WriteTo||Writes the current node to a specified XmlWriter.|
Figure 4 – XmlNode Methods
Some of this methods will be demonstrated in later lessons about writing and reading XML documents.
The XmlDocument class represents the actual XML document and all its contents. The following are some properties exclusive to this class.
|DocumentElement||The root element of the document.|
|PreserveWhitespace||Tells whether to preserve white spaces in the content of the document.|
The following are some methods exclusive to XmlDocument class.
|CreateAttribute||Creates an XmlAttribute object.|
|CreateComment||Creates an XmlComment object.|
|CreateElement||Creates an XmlElement object.|
|CreateTextNode||Creates a XmlText object with the specified text.|
|CreateXmlDeclaration||Creates an XmlDeclaration with the specified values.|
|GetElementById||Gets the XmlElement with the specified ID.|
|GetElementsByTagName||Returns a list of descendant elements that matches the specified name.|
|Load||Loads an XML document from a file.|
|LoadXml||Loads an XML from a string.|
|Save||Saves the content of this XmlDocument into a specified file.|
Notice the methods which starts with Create. This methods are used to create XML DOM objects. For example, if you want to create an element for the current document, then you use the CreateElement method. These is because using the DOM classes’ constructors are prohibited due to its protection level. Most of these methods will be demonstrated in later lessons.
The XmlElement class represents a single XML element. The following are some useful properties of the XmlElement class.
|Attributes||Contains all the attributes of an element.|
|HasAttributes||Tells whether the element contains at least one or more attributes.|
|IsEmpty||Specifies whether this element, when empty, should be written in short tag format (<element />) or long tag format (<element></element>). A value of true uses short tag format and false for long tag format.|
|Value||The text between the opening tag and closing tag of this element. If it contains child elements, then this property will be empty.|
The following are some methods exclusive to XmlElement.
|GetAttribute||Returns the value of the attribute with the specified name.|
|GetAttributeNode||Returns the XmlAttribute with the specified name.|
|GetElementsByTagName||Returns a list of child elements that matches the specified name.|
The XmlText and XmlComment classes represents a text inside an element and a comment respectively. They are too simple to have their own sections in this lesson so I decided not to discuss them in detail.