The Nodes of an XML Element


Introduction to XML Nodes

Consider the following example of an XML file named Videos.xml:

<?xml version="1.0" encoding="utf-8" ?>
		<Title>The Distinguished Gentleman</Title>
		<Director>Jonathan Lynn</Director>
		<Length>112 Minutes</Length>
		<Title>Her Alibi</Title>
		<Director>Bruce Beresford</Director>
		<Length>94 Mins</Length>
		<Title>Chalte Chalte</Title>
		<Director>Aziz Mirza</Director>
		<Length>145 Mins</Length>

An XML file appears as an upside-down tree: it has a root (in this case <Videos>), it can have branches (in this case <Video>), and it can have leaves (an example in this case is <Title>). As we have seen so far, all of these objects are created using the same technique: a tag with a name (such as <Title>) and an optional value. Based on their similarities, each of these objects is called a node. To support nodes of an XML file, the .NET Framework provides the XmlNode class, which is the ancestor to all types of nodes. XmlNode is an abstract class without a constructor. Based on this, to get a node, you must have an object that would produce one and you can only retrieve a node from an (existing) object.


The Distinguished Gentleman


Introduction to Node Types

To make XML as complete and as efficient as possible, it can contain various types of nodes. The categories or possible types of nodes are identified by an enumeration named XmlNodeType. If you use an XmlTextReader object to scan a file, when calling Read(), the class has a property named NodeType that allows you to identify the node that was read. NodeType is a read-only property of type XmlNodeType and it is declared as follows:

public override XmlNodeType NodeType { get; }

Therefore, when calling the XmlTextReader.Read() method, you can continuously check the value of the XmlTextReader.NodeType property to find out what type of node was just read, and then you can take an appropriate action.


Previous Copyright © 2006-2016, FunctionX, Inc. Next