Traversing the DOM Tree : DOM « XML « ASP.Net

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » XML » DOM 
Traversing the DOM Tree

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        string xmlPath = MapPath("Books.xml");
        XmlDocument doc = new XmlDocument();
        //doc.Load(xmlPath);
        doc.LoadXml("<employees>" +
                    "<employee id='1'>" +    
                    "<name><firstName>First Name</firstName>" 
                    "<lastName>Last Name</lastName>" +                       
                    "</name><city>City</city>" +
                    "<state>WA</state><zipCode>99999</zipCode>" +
            "</employee></employees>");         
        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>");
        else {
            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);
        }
    }
</script>

<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>

           
       
Related examples in the same category
1.Use XML Document (DOM) to select one Node
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.