<h4>How Do I...Load and use the XmlDocument (W3C DOM)?</h4>
<div class="indent" style="width:660">
This sample illustrates the classes which implement the
<A href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html">W3C Document object Model (DOM) level 1 Core</A></A> and the
<A href="http://www.w3.org/TR/DOM-Level-2/core.html">Core DOM Level 2</A></A>. The DOM is an in-memory (cache) tree representation of an XML document and
enables the navigation and editing of this document.</div>
There are a number of classes implemented as part of the W3C DOM. Of these the <b>XmlNode</b> provides methods and properties to manipulate a node. Many of the other W3C classes
are specializations of the XmlNode class. The <b>XmlDocument</b> class represents the XML document and has a <b>Load</b> method to load the document from a file,
stream or an XmlReader. The list of classes is given in the table below.</div>
To navigate the document, the DisplayTree method recursively iterates over the XmlDocument using the <b>HasChildNodes</b> and <b>FirstChild</b> properties
to move down the tree.
The <b>NextSibling</b> property moves to the node immediately next to the current node, returning null if there is no node to move to.
</div>
<div class="code">
<xmp>
public void DisplayTree(XmlNode node)
{
if (node != null)
Format (node);
if (node.HasChildNodes)
{
node = node.FirstChild;
while (node != null)
{
DisplayTree(node);
node = node.NextSibling;
}
}
}
</xmp></div>
<div class="indent" style="width:660">
The <b>Name</b> property gives the name of the node, and the <b>Value</b> property its value depending on the type.
The table below shows the value returned for a given node type.</div>
The Format method, shown below, display the details of the current node using the Name and Value properties. For <b>XmlElement</b> node types the <b>Attributes</b>
property provides a list of attributes as a <b>XmlNamedNodeMap</b> class. This class contains <b>GetNamedItem</b> to retrieve an XmlNode specified by name and
<b>SetNamedItem</b> to add an XmlNode using it's Name property. It also implements IEnumerable in order to support the 'foreach' statement.</div>
The XmlNode class also has a <b>NodeType</b> property. See <A target=content href="ReadXMLFile.aspx">How Do I...Read XML from a file?</A> for a table of
the different node types. The output from running this sample is shown below.
</div>
<div class="code">
<xmp>
Reading file books.xml ...
File books.xml read sucessfully ...
XmlDocument loaded with XML data successfully ...
Processing ...
bookstore
book genre<autobiography> publicationdate<1981> ISBN<1-861003-11-0>
title
#text<The Autobiography of Benjamin Franklin>
author
first-name
#text<Benjamin>
last-name
#text<Franklin>
price
#text<8.99>
book genre<novel> publicationdate<1967> ISBN<0-201-63361-2>
title
#text<The Confidence Man>
author
first-name
#text<Herman>
last-name
#text<Melville>
price
#text<11.99>
book genre<philosophy> publicationdate<1991> ISBN<1-861001-57-6>
title
#text<The Gorgias>
author
name
#text<Plato>
price
#text<9.99>
</xmp></div>
<H4>Summary</H4>
<OL>
<LI>The XmlDocument, XmlNode and other classes implement the <A href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html">W3C Document object Model level 1 Core</A></A>
and the <A href="http://www.w3.org/TR/DOM-Level-2/core.html">Core DOM Level 2</A></A> specifications.
<LI>The XmlDocument is an in-memory (cache) tree representation of an XML document.
<LI>There are different node types specializing from XmlNode to enable you to manipulate the XML document.
<LI>You can get faster, non-cached, forward only stream access to XML using the XmlTextReader and XmlTextWriter classes.