XmlReader
is an abstract base class that provides an event-based, read-only,
forward-only XML pull parser (I’ll discuss each of
these terms shortly). XmlReader
has three concrete
subclasses, XmlTextReader
,
XmlValidatingReader
, and
XmlNodeReader
, which enable you to read XML from a
file, a Stream
, or an XmlNode
.
You can also extend XmlReader
to read other,
non-XML data formats, and deal with them as if they were XML
(you’ll learn how to do this in Chapter 4).
The base XmlReader
provides only the most essential functionality for reading XML
documents. It does not, for example, validate XML
(that’s what XmlValidatingReader
does) or expand XML entities into their respective character data
(though XmlTextReader
does). This does not mean
that XML read from a text file cannot be validated at all; you can
validate XML from any source by using the
XmlValidatingReader
constructor that takes an
XmlReader
object as a parameter, as
I’ll demonstrate.
Here are those four terms I used to describe
XmlReader
again, with a little explanation.
An
event in a stream-based XML reader indicates the start or end of an
XML node as it is read from the data stream. The
event’s information is delivered to your
application, and your application takes some action based on that
information. In XmlReader
, events are delivered by
querying XmlReader
’s properties
after calling its Read( )
method.
XmlReader
, as its
name implies, can only read XML. For writing XML, there is an
XmlWriter
class, which I will discuss in Chapter 3.
Once a node has been read from an XML
document, you cannot back up and read it again. For random access to
an XML document, you should use XmlDocument
(which
I’ll discuss in Chapter 5) or
XPathDocument
(which I’ll discuss
in Chapter 6).
Pull parsing is a more complex concept, which I’ll describe in detail in the next section.
3.137.178.133