SUMMARY
This article provides a roadmap for programming XML with
the pull-model parser of the .NET Framework. To help with learning a Microsoft
product or technology, roadmap articles provide links to useful information,
including online documentation, Microsoft Knowledge Base articles, and white
papers.
Overview
The .NET Framework provides two ways to parse XML data:
- Pull-model parser (XmlReader and related classes)
- DOM-model parser (XmlDocument and related classes)
The pull model provides forward-only, read-only, noncached
access to XML data. The DOM model provides in-memory, random, read/write access
to XML data. This article covers the pull-model parser of the .NET Framework.
This implementation is compliant with the World Wide Web Consortium (W3C) XML
1.0 and the Namespaces recommendations.
The pull model is implemented
with classes based on the
XmlReader class.
XmlReader provides read access to XML data, and also enables you to skip
data that you do not want. Note that the pull model has several advantages over
the Simple API for XML (SAX) model, as explained at the following MSDN Web
site:
SAX is not implemented in .NET Framework, although it can be
built by using the
XmlReader class.
XML in the .NET Framework is implemented under
the
System.Xml namespace. The following classes are related with the pull model:
- The XmlReader class is the abstract base class from which all other reader
classes are derived. You can also implement your own reader class with XmlReader as the base.
- The XmlTextReader class represents a forward-only, noncached reader to access XML
data. It lets you read data from different resources such as files, URLs, and
streams.
- The XmlNodeReader class represents a parser over an XmlNode of a DOM document; this is typically used to read a specific
section of a DOM tree.
- The XmlValidatingReader class represents a validating reader. It lets you validate XML
data against DTD, XDR, and XSD schemas. Note that this is the only class that
provides the validation feature.
- The XmlResolver class is used to resolve external resources, such as entities and
schemas named by a URI.
System.Xml also provides several other classes, such as
XmlNameTable,
XmlDeclaration,
XmlSpace, and
XmlWhitespace, that are compliant with World Wide Web Consortiuem (W3C)
specifications. For more information, see the SDK documentation.
Key Concepts and QuickStarts
The best place to start is with the following SDK documention
link:
Follow the links in the "See Also" section for samples of reading
elements and attributes, validating against schemas, and skipping content that
you do not want.
QuickStart tutorials are a quick way to understand
what the .NET Framework technology offers leading-edge developers. Following
are links to samples that are relevant to
XmlReader class:
back to the top
Knowledge Base Articles
Microsoft Knowledge Base How To articles provide step-by-step
instructions for accomplishing specific tasks.
Typical steps in
pull-model parsing include instantiating one of the
XmlReader-based classes, based on the application's requirements, and
reading the data after setting up the required properties.
For
example, if performance is the key factor and no validation is required,
XmlTextReader is a good choice. With the use of
MoveToContent and
Skip methods, it enables reading only the required data with improved
performance. The following Knowledge Base articles provide code samples for
programming
XmlReader-related classes.
301225 HOW TO: Read XML from a File by Using Visual Basic .NET
307548 HOW TO: Read XML from a File by Using Visual C# .NET
301232 HOW TO: Read XML Data from a URL by Using Visual Basic .NET
307643 HOW TO: Read XML Data from a URL by Using Visual C# .NET
315533 HOW TO: Use DTDs, XDR, or XSD to Validate an XML Document in Visual Basic .NET
307379 HOW TO: Use DTDs, XDR, or XSD to Validate an XML Document in Visual C# .NET
317463 HOW TO: Validate XML Fragments Against an XML Schema in Visual Basic .NET
318504 HOW TO: Validate XML Fragments Against an XML Schema in Visual Basic .NET
317595 HOW TO: Validate an XML Document That Uses Multiple Schemas
301228 HOW TO: Read XML Data from a Stream in .NET Framework SDK
You can also run XSL transformations (XSLT), query
and modify XML data by using DOM, and serialize or deserialize XML data by
using the
System.Xml classes in .NET Framework. For more information, see the
following Knowledge Base article:
313651 INFO: Roadmap for XML in the .NET Framework
Guidelines
Your choice of parser may depend on your application's
requirements. Note that the DOM-model parser loads the whole XML document into
memory whereas the pull model loads one node at a time. The pull model consumes
less memory but does not provide random access to data.
The DOM
model is suited for applications that require random, read/write access to the
data where memory consumption is not a factor. The pull model is suited for
applications that require speed and memory conservation. Under many
circumstances, the required solution may be a hybrid of these two models. For
example, if part of a very large XML document needs to be manipulated, it may
be efficient to use the pull model to read it, and then construct a DOM with
only the data needed for additional modification.
Troubleshooting
If you experience problems and want assistance, the best place to
start is at the Microsoft Product Support Services (PSS) Web site:
You have several options to find answers to your questions. You
can search the Microsoft Knowledge Base, post your questions or share your
experiences on Microsoft newsgroups, obtain the latest service packs and news
about a specific product, or contact Microsoft Product
Support.