Listing Traversing DOM Tree Using XmlDocument Class
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
string xmlPath = Request.PhysicalApplicationPath +
@"\App_Data\Books.xml"; XmlDocument doc = new XmlDocument(); doc.Load(xmlPath);
XmlNode rootNode = doc.DocumentElement; DisplayNodes(rootNode);
void DisplayNodes(XmlNode node) {
//Print the node type, node name and node value of the node if (node.NodeType == XmlNodeType.Text) {
Response.Write("Type= [" + node.NodeType+ "] Value=" + node.Value + "<br>");
Response.Write("Type= [" + node.NodeType+"] Name=" + node.Name + "<br>");
//Print attributes of the node if (node.Attributes != null) {
XmlAttributeCollection attrs = node.Attributes;
foreach (XmlAttribute attr in attrs) {
Response.Write("Attribute Name = " + attr.Name + "Attribute Value = " + attr.Value);
//Print individual children of the node XmlNodeList children = node.ChildNodes;
foreach (XmlNode child in children) {
DisplayNodes(child);
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Traversing the DOM Tree</title> </head> <body>
<form id="form1" runat="server"> <div> </div> </form> </body> </html>
As you can see from Listing 6-3, the core class that forms the root of this tree is the XmlDocument class. This code loads the XmlDocument with data from the books.xml file and uses that as the basis to traverse the document.
The XmlDocument is first instantiated, and a file URL is passed to it. The document loads the XML from the file and automatically generates the DOM tree.
XmlDocument doc = new XmlDocument(); doc.Load(xmlPath);
Next, you get a handle to the root node of the document tree:
XmlNode rootNode = doc.DocumentElement;
After the root node is obtained, the DisplayNodes() method is then invoked to recursively traverse through all the children of that node. DisplayNodes() is generic enough to print details of any node type. Remember that the DOM tree consists of nodes of different types (elements, attributes, processing instructions, comments, text nodes, and so on). This example just prints the generic information about the node (name, type); if it's a text node, it prints the value of the node as well.
if (node.NodeType == XmlNodeType.Text) {
Response.Write("Type= [" + node.NodeType+ "] Value=" + node.Value + "<br>");
Response.Write("Type= [" + node.NodeType+"] Name=" + node.Name + "<br>");
Next, the code prints any attributes associated with the node.
XmlAttributeCollection attrs = node.Attributes;
foreach (XmlAttribute attr in attrs) {
Response.Write("Attribute Name =" + attr.Name + "Attribute Value =" + attr.Value);
The last step gets all the children of the current node and calls DisplayNodes() on each of the children. Note that the ChildNodes method gets only the direct children of the node. To get all children of a node, you must use recursive code as follows.
XmlNodeList children = node.ChildNodes;
foreach (XmlNode child in children) {
DisplayNodes(child);
Navigate to the page from a browser and you should see something similar to Figure 6-4.
|
|Traversing tue DOM Tree - Microsoft internet Explorer |
BEI 131 | |
|
File Edit View Favorites lools Help | ||
|
Q Back - * Jl ¿1 < Search Favories Media & ' - u^ * . ¿X | ||
|
Address (.-¡¿] hllp://locahost/MjftrojeclsAVrow/Chaptet 6/3_TraverseDOM T lee. aspx |
_d 0Go |
Links " |
|
Type= [Element] Name=bookstore |
- | |
|
Type= [Element] Name=book | ||
|
Attribute Name =genreAitnbute Value =autobiographyType= [Element) Name=htie | ||
|
Type= [Text] V alue=The Autobiography of Benjamin Franklin | ||
|
Type= [Element] Name=author | ||
|
Type= [Element] Name=firsE-name | ||
|
Type= [Text] Value=Benjamin | ||
|
Type= [Element] Name=iast-name | ||
|
Type= [Text] Value=Franklin | ||
|
Type= [Element] Name=pnce | ||
|
Type= [Text] Value=8.99 | ||
|
Type= [Element] Name=book | ||
|
Attribute Name =genreAttnbute Value =novelType= [Element] Name=tiile | ||
|
Type- [Text] Value=The Confidence Man | ||
|
Type= [Element] Name=author | ||
|
Type= [Element] Name=Srsfc-name | ||
|
Type- [Text] Value^Herman | ||
|
Type= [Element] Name^ast-name | ||
|
Type= [Text] Value^Melville | ||
|
Type= [Element] Name=pnce | ||
|
Type= [Text] Value=l 1.99 | ||
|
Type= [Element] Name=book | ||
|
Attribute Name =genreArtribute Value =philosophyType= [Element] Name=tide | ||
|
Type= [Text] Value=The Gorgias | ||
|
Type= [Element] Name=autSior | ||
|
Type= [Element] Name=name | ||
|
Type= [Text] Value=P!ato | ||
|
Type= [Element] Name=price | ||
|
Type= [Text] Value=9.99 | ||
|
_M | ||
|
Qoone |
ij Local ntranet | |
Post a comment