The sample code shown below constructs an XmlTextReader.
<div class="code"><xmp>
XmlTextReader reader = new XmlTextReader ("books.xml");
</xmp></div>
<div style="width:660">
Once loaded, the XmlTextReader moves across the XML data by performing sequential reads to get the next record using the <b>Read</b> method. It returns false
if there are no more records.
<div class="code">
<xmp>
while (reader.Read())
{
// Do some work here on the data
}
</xmp></div>
<div style="width:660">
To processes the XML data, each record has a node type which can be determined from the <b>NodeType</b> property. The <b>Name</b> and <b>Value</b> properties return
the node name (e.g. the element and attribute names) and the node value (i.e. the node text) of the current node (or record). The code sample below uses these
properties to display the details about the node for Element and DocumentType types. The node type is determined by the NodeType enumeration shown
in the table.
<div class="code"><xmp>
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an Element
<div>Table of NodeTypes which are equivalent to the W3C DOM node types with some extended types required for forward only reading.</div><br clear="left"><br>
<td width="1000" height="19"><font size="1">Returned when the reader has gotten to the end of the entity replacement as a result of a call to ExpandEntity().</font></td>
The <b>Depth</b> property reports the depth of the current node and can be useful for formatting. Nodes at the root level are at depth 0. Combining this with
the Name and Value properties we can create a sample which processes an XML file and formats the output depending on the node type and the depth,
gathering statistics as it reads. The Format method, shown below, implements some basic formatting code to the console. The full code is at
// Display the attributes values for the current node
if (reader.HasAttributes)
{
Console.Write(" Attributes:");
for (int j=0; j < reader.AttributeCount; j++)
{
Console.Write(" [{0}] " + reader[j], j);
}
}
Console.WriteLine();
}
</xmp></div>
<div style="width:660">
The <b>Prefix</b> property returns the namespace prefix associated with the node. Element node types can have a list of attribute nodes associated with them.
Here we test whether the node has any attributes with the <b>HasAttributes</b> property and then use the node index operators to retrieve each attribute value.
This is analogous to a collection of attributes for the node.
The <b>AttributeCount</b> property returns the number of attributes for the current node. This approach is used if all you are interested in are the attribute
values and are not concerned with other properties of the attribute nodes (e.g. The name of the attribute).
In the <A target=content href="ReadXmlStream.aspx">How Do I...Read XML from a stream?</a> topic we show an alternative approach to accessing the attributes
by moving to each attribute node in order to read both its name and value.</div>