Using the XmlReader

The .NET Framework provides three implementations of XmlReader: XmlTextReader, XmlValidatingReader, and XmlNodeReader. In this section, I’ll present each class one at a time and show you how to use them.

XmlTextReader

XmlTextReader is the most immediately useful specialization of XmlReader. XmlTextReader is used to read XML from a Stream, URL, string, or TextReader. You can use it to read XML from a text file on disk, from a web site, or from a string in memory that has been built or loaded elsewhere in your program. XmlTextReader does not validate the XML it reads; however, it does expand the general entities &lt;, &gt;, and &amp; into their text representations (<, >, and &, respectively), and it does check the XML for well-formedness.

In addition to these general capabilities, XmlTextReader can resolve system- and user-defined entities, and can be optimized somewhat by providing it with an XmlNameTable. Although XmlNameTable is an abstract class, you can instantiate a new NameTable, or access an XmlReader’s XmlNameTable through its NameTable property.

Tip

An XmlNameTable contains a collection of string objects that are used to represent the elements and attributes of an XML document. XmlReader can use this table to more efficiently handle elements and attributes that recur in a document. An XmlNameTable object is created at runtime by the .NET parser every time it reads an XML document. If you are parsing many documents with the same format, using ...

Get .NET & XML now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.