Introduction to Node Types in XML
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.
Elements Fundamentals
Introduction
An element in an XML document is an object that begins with a start-tag, may contain a value, and may terminate with an end-tag. Based on this, the combination of a start-tag, the value, and the end-tag is called an element. An element can be more than that but for now, we will consider that an element is primarily characterized by a name and possibly a value.
To support XML elements, the System.Xml namespace provides the XmlElement class. XmlElement is based on a class named XmlLinkedNode that itself is based on XmlNode. To access an XML element, you can declare a variable of type XmlElement but the main purpose of this class is to get an element from a DOM object. For this reason, the XmlElement class doesn't have a constructor you can use. Instead, and as we will learn, the other classes have methods that produce an XmlElement element you can manipulate as necessary.
In the previous lesson, we saw that every XML file must have a root and we mentioned that you could call the XmlDocument.DocumentElement property to access it. This property is of type XmlElement and, to access it, you can declare an XmlElement variable and assign it this property. Here is an example:
using System;
using System.IO;
using System.Xml;
namespace VideoCollection1
{
class Program
{
static int Main(string[] args)
{
string strFilename = "Videos.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(strFilename))
{
xmlDoc.Load(strFilename);
XmlElement elm = xmlDoc.DocumentElement;
Console.WriteLine("{0}", elm);
}
else
Console.WriteLine("The file {0} could not be located",
strFilename);
Console.WriteLine();
return 0;
}
}
}
This would produce:
System.Xml.XmlElement
Press any key to continue . . .
An XML element is represented in the XmlNodeType enumeration as the Element member. When using the Read() method of an XmlTextReader object, to find out if the item being read is an element, you can check whether the member of the current XmlNodeType is Element. Here is an example:
using System;
using System.IO;
using System.Xml;
namespace VideoCollection1
{
class Program
{
static int Main(string[] args)
{
string strFilename = "Videos.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(strFilename))
{
XmlTextReader rdrXml = new XmlTextReader(strFilename);
do {
switch (rdrXml.NodeType)
{
case XmlNodeType.Element:
break;
}
}while (rdrXml.Read());
}
else
Console.WriteLine("The file {0} could not be located",
strFilename);
Console.WriteLine();
return 0;
}
}
}