XmlDocument and XmlNode Basics PDF Print E-mail
User Rating: / 0
PoorBest 
Tuesday, 10 March 2009
HTML clipboard
The XmlDocument class gives you the ability to load an Xml file. The class implements the DOM, which is an in-memory (cache) tree representation of an XML document, which allows you to navigate and edit the document.

The XmlDocument inherits from the XmlNode class, as seen in the class browser. The XmlNode class in reality provides much of the underlying functionality since it is the base class in the .NET implementation of the DOM.

So the XmlNode represents a single node in the Xml, while the XmlDocument extends the XmlNode class to represent an entire Xml Document (hence the name, XmlDocument).

Once the Xml file is loaded, you can also locate the root node of the document.

Source Example

In my example source code, I load the Xml Document using the XmlDocuments Load method. I then initialize an XmlNode object with the root element of the Xml. I then select all the nodes that match the path /books/category/title in my xml file followed by outputting the entire node and its child nodes in a concatenated fashion using the InnerText property.

The Xml File

<?xml version="1.0" encoding="utf-8" ?>
<books>
<category name="dotnet">
<title>
<name>ASP.NET in 15 seconds</name>
<author>Salman Ahmed</author>
<price>$34.99</price>
<description>This book will teach you everything there is to know about ASP.NET</description>
<isbn>123ABC456DEF789GHI</isbn>
</title>
<title>
<name>C# in 15 seconds</name>
<author>Salman Ahmed</author>
<price>$44.99</price>
<description>This book will teach you everything there is to know about 
Microsoft's latest technology, csharp</description>
<isbn>123ABC456DEF789GHI</isbn>
</title>
</category>
</books>
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials