Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Parsing XML With The XMLDocument Object
Parse XML documents in ASP.NET with the XMLDocument object.

| Delving Deeper |

As you must have figured out by now, using the DOM parser is fairly easy - essentially, it involves creating a "tree" of the elements in the XML document, and traversing that tree with built-in methods. In the introductory example, I ventured as far as the document element; in this next one, I'll go much further, demonstrating how the parser's built-in methods can be used to navigate to any point in the document tree.


<%@ Page Language="C#"%>
<%@ import  namespace="System.Xml"%>
<html>
<head>
<script runat="server">
void NodeDetails(XmlNode objNode) {

// print details of the root element
output.Text = "Node Name: " + objNode.Name + "<br />";

// check if the element has child nodes
if(objNode.HasChildNodes) {

// get the list of child nodes
XmlNodeList objChildren = objNode.ChildNodes;

// loop through the Nodes
for(int count = 0; count < objChildren.Count; count++) {

output.Text += "&nbsp;&nbsp;&nbsp;Node Name: " + objChildren[count].Name + "<br />";

// check for children
if(objChildren[count].HasChildNodes) {

// get the list of child nodes
XmlNodeList objChildrenOfChildren = objChildren[count].ChildNodes;

// loop through the Nodes
for(int innercount = 0; innercount < objChildrenOfChildren.Count; innercount++) {

output.Text += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node Name: " + objChildrenOfChildren[innercount].Name + "<br />";
}
}
}
}
}

void Page_Load() {

// location of XML file
string strXmlDoc = "http://localhost:2121/xml/menu.xml";

// create an instance of XmlDocument object
XmlDocument objXmlDoc = new XmlDocument();

// load XML document in XmlDocument object
objXmlDoc.Load(strXmlDoc);

// get the root element
XmlElement objRootElem = objXmlDoc.DocumentElement;

// get more details about the node
NodeDetails(objRootElem);

}
</script>
</head>
<body>
<asp:label id="output" runat="server" />
</body>
</html>


Here's the output.

Output image

As demonstrated in the first example, the fundamentals remain unchanged - create an instance of an XmlDocument object, load an XML document, get a reference to the root of the tree and start traversing the tree. Consequently, most of the code here remains the same as that used in the introductory example, with the changes occurring only in the NodeDetails() function. Let's take a closer look at this function:


void NodeDetails(XmlNode objNode) {

// print details of the root element
output.Text = "Node Name: " + objNode.Name + "<br />";

// check if the element has child nodes
if(objNode.HasChildNodes) {

// get the list of child nodes
XmlNodeList objChildren = objNode.ChildNodes;

// loop through the Nodes
for(int count = 0; count < objChildren.Count; count++) {

output.Text += "&nbsp;&nbsp;&nbsp;Node Name: " + objChildren[count].Name + "<br />";

// check for children
if(objChildren[count].HasChildNodes) {

// get the list of child nodes
XmlNodeList objChildrenOfChildren = objChildren[count].ChildNodes;

// loop through the Nodes
for(int innercount = 0; innercount < objChildrenOfChildren.Count; innercount++) {

output.Text += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Node Name: " + objChildrenOfChildren[innercount].Name + "<br />";
}
}
}
}
}


Once a reference to the root of the tree has been obtained and passed to NodeDetails(), the "ChildNodes" property is used to obtain a list of the children of that node. This list is returned as a new XmlNodeList object, which comes with its own properties and methods for accessing individual elements of the node list.

As you can see, one of these properties is the "Count" property, used to obtain the number of child nodes, in order to iterate through them. Individual elements of the node list can be accessed in the same manner that you would access individual elements of an array. Each element in the XmlNodeList object returns a XmlNode object, which puts us back on familiar territory - the XmlNode object's standard "Name" property can now be used to access information about the node.

The process is then repeated for each of these XmlNode objects - a check for further children, a retrieved XmlNodeList object, a loop iterating through the child nodes - until the end of the document tree is reached.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback