XML Classes in the .NET Framework





You can also use XmlReader classes to determine various factors such as the depth of a node in an XML
document, whether the node has attributes, the number of attributes in a node, and the value of an
attribute. To perform validation while reading the XML data using the XmlReader class, use the new


System.Xml.XmlReaderSettings class that exposes properties which can be set to appropriate values
to validate XML data using DTD and XSD schemas. The XmlReaderSettings class is described in
detail in Chapter 5.


Although the .NET Framework includes concrete implementations of the XmlReader class, such as the


XmlTextReader, XmlNodeReader, and the XmlValidatingReader classes, the recommended
practice in .NET Framework 2.0 is to create XmlReader instances using the Create() method. The


XmlReader object returned by the Create method has better conformance checking and compliance to
the XML 1.0 recommendation.


SAX Vs XmlReader


When you first look at it, the .NET Framework class library’s implementation of forward-only access
seems very similar to the Simple API for XML (SAX), but actually they are fundamentally different.
Where SAX uses a more complex push model, the class library uses a simple pull model. This means
that a developer requests or pulls data one record at a time instead of having to capture the data using
event handlers. Coding using the .NET Framework class library’s implementation of forward-only
access is more intuitive because you can handle the processing of an XML document as you would a
simple file, using a good old-fashioned while loop. There is no need to learn about event handlers or
SAX’s complex state machine.


Random Access via DOM


The XML DOM class, System.Xml.XmlDocument, is a representation of the XML document in memory.
The .NET Framework DOM implementation provides classes that enable you to navigate through an
XML document and obtain relevant information. Every XML document consists of parent and child
nodes. The XmlDocument class has the capability to read in XML files, streams, or XmlReader objects.
Among the many public methods of the XmlDocument class, you will want to start with the Load()


method. Using this method, you can easily load XML data into an XmlDocument object.


Choosing the Right XML Reader


Basically, you could take an either/or approach when you choose your XML class for reading. For example,
either you choose the XmlReader class for fast, forward-only, read-only, non-cached type reading. Or you
can choose the DOM class XmlDocument for full-featured XML document reading and manipulation. The
major deciding factors for choosing one method over the other are whether all data needs to be in memory
at one time (large files take up large amounts of memory, which in many cases isn’t a good thing) and
whether random access to the data is needed. When either of these factors is a requirement, the DOM tree
should probably be used because the process of repeatedly reading forward sequentially through a docu-
ment to find the right place in the stream of XML to read, update, or write random data is time consuming.


Is XML API in .NET a Replacement for MSXML 6.0?


Microsoft’s XML parser, MSXML 6.0 (now known as Microsoft XML Core Services), has historically
provided much of the XML DOM support described in this section. The .NET XML managed objects
largely overlap the functionality exposed in the COM-based MSXML 6.0 library. Generally, you want to
use the managed objects offered in the various .NET XML namespaces. There are occasions, however,
when you should use the MSXML 6.0 implementation when you need backward compatibility with