SUMMARY
This article provides a roadmap for programming XML with
the Document Object Model (DOM) 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.
This article assumes a familiarity with the Worldwide Web
Consortium (W3C) DOM standard.
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 DOM-model parser of the .NET Framework.
This implementation is compliant with the Worldwide Web Consortium (W3C) DOM
Level 1 and Level 2 specifications. The DOM-model parser lets you
programmatically manipulate the in-memory representation of the XML
data.
XML in the .NET Framework is implemented under the
System.Xml namespace. The following DOM-related classes provide ways to
access, modify, and delete content in XML documents by using the DOM model:
- The XmlNode class represents a single node. As shown in the
DOM
hierarchy diagram, this abstract class acts as the base class for
most of the DOM classes. (This diagram also shows the equivalent W3C names of
the DOM classes.) XmlNode contains the majority of methods and properties that are needed
to work with the data in an XML node. The XmlNodeList class represents a set of ordered nodes. The XmlNamedNodeMap class can be used to handle unordered sets of nodes.
- The XmlDocument class represents the DOM document. This is an in-memory
representation of XML data that enables navigation and editing, and can be used
to load and manipulate XML documents. The XmlNodeChangedEventArgs class handles event handlers registered on an XmlDocument.
- The XmlAttribute class represents an attribute; this class lets you access and
edit attributes of the nodes. The XmlAttributeCollection class represents a collection of attributes.
- The XmlElement class represents an element. This class is derived from XmlLinkedNode, which provides access to previous and subsequent
nodes.
System.Xml also provides several other classes, such as
XmlText,
XmlDeclaration,
XmlComment, and
XmlWhitespace, that map to a W3C DOM structure. For a complete list, see "Types
of XML Nodes" at the following MSDN Web site:
Key Concepts and QuickStarts
The QuickStart tutorials are the fastest way to understand what
the .NET Framework technology offers leading-edge developers. Following are
links to relevant samples and documentation for DOM-model processing of XML.
HOW TO Articles
Microsoft Knowledge Base HOW TO articles provide step-by-step
instructions for accomplishing specific tasks.
Typical steps in
DOM-model processing include loading the XML data into an
XmlDocument instance, manipulating or querying the data by using the DOM
classes, and then persisting the results. The following Knowledge Base articles
provide code samples for performing these activities:
317661 HOW TO: Load and Save XML by Using DOM in .NET Framework with Visual Basic .NET
317662 HOW TO: Load and Save XML by Using DOM in .NET Framework with Visual C# .NET
317663 HOW TO: Access XML Data Using DOM in .NET Framework with Visual Basic .NET
317664 HOW TO: Access XML Data Using DOM in .NET Framework with Visual C# .NET
317665 HOW TO: Modify XML Data by Using DOM in .NET Framework with Visual Basic .NET
317666 HOW TO: Modify XML Data by Using DOM in .NET Framework with Visual C# .NET
You can also run XSL transformations (XSLT),
validate XML documents against schemas, and serialize and deserialize XML data
by using the
System.Xml classes in .NET Framework.
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
313651 INFO: Roadmap for XML in the .NET Framework
Guidelines
Your choice of parser models 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 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.