Login  |  Register
Home | Articles | Downloads | Resources | About
Contact Us | My Resume
   Topics:  ASP.NET  C#  .NET  XML 
XML
Read XML Data Recursively

Posted by on Wednesday, March 03, 2004 (PST)

This function will read the data from XML file recursively by traversing through the XML data and will display the text and url value.

using System;
using System.Xml;
class Test
{
    static void Main(string[] args)
    {
        ReadXMLRecursively rd = new ReadXMLRecursively();
        rd.Run("XMLData.xml");
    }
    public class ReadXMLRecursively
    {
        public void Run(string doc)
        {
            XmlTextReader reader = null;
            try
            {
                // Load the XML from file
                reader = new XmlTextReader(doc);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                // Create an XmlDocument from the XmlTextReader
                XmlDocument myXmlDocument = new XmlDocument();
                myXmlDocument.Load (reader);
                // Start from the item Element
                ParseTree(myXmlDocument.DocumentElement);
            
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        
        // Recursive function
        // This function will will recursively traverse 
        // through the XML file and display all the 
        // node and element values
        private void ParseTree(XmlNode node)
        {
            if (node != null)
            {
                if (node.HasChildNodes == true)
                {
                    if (XmlNodeType.Element == node.NodeType)
                    {
                        XmlNamedNodeMap map = node.Attributes;
                        foreach (XmlNode attrnode in map)
                        {
                            if (attrnode.Name == "text")
                            {
                             Console.Write("Text : " + attrnode.Value + " - " );
                            }
                            if (attrnode.Name == "url")
                            {
                             Console.Write("Url : " + attrnode.Value + "\n");
                            }
                        }
                    }

                    node = node.FirstChild;
                    while (node != null)
                    {
                        ParseTree(node);
                        node = node.NextSibling;
                    }
                
                } 
                else 
                {
                    if (node.HasChildNodes == false)
                    {
                        if (node.Attributes != null) 
                        {
                            XmlAttributeCollection attrs = node.Attributes;
                            foreach (XmlAttribute attr in attrs) 
                            {
                                Console.Write("\t");
                                if (attr.Name == "text")
                                {
                                 Console.Write("Text : " + attr.Value + " - ");
                                }
                                if (attr.Name == "url")
                                {
                                 Console.Write("Url : " + attr.Value + "\n");
                                }
                            }
                        }
                    }                    
                }
            }
        }
    }
}

XML file Data

<?xml version="1.0" encoding="utf-8" ?> 
<menu>
    <item text="File" url="folder">
        <item text="Open" url="test.htm"></item>
        <item text="Save" url="test.htm"></item>
        <item text="Close" url="test.htm"></item>
    </item>
    <item text="View" url="test.htm"></item>
    <item text="Help" url="folder">
        <item text="Contents" url="test.htm"></item>
        <item text="Search" url="folder">
            <item text="Search file" url="test.htm"></item>
            <item text="Search directory" url="test.htm"></item>
            <item text="Search Text" url="folder">
                <item text="Find" url="test.htm"></item>
                <item text="Replace" url="test.htm"></item>
            </item>
        </item>
        <item text="About" url="test.htm"></item>
    </item>
    <item text="XML" url="folder">
        <item text="View XML" url="test.htm"></item>
    </item>
    <item text="Window" url="test.htm"></item>
    <item text="Tools" url="test.htm"></item>
    <item text="Debug" url="test.htm"></item>
    <item text="Build" url="test.htm"></item>
    <item text="Favorites" url="test.htm">
        <item text="Add to Favorites" url="test.htm"></item>
        <item text="Organize Favorites" url="test.htm"></item>
        <item text="News" url="test.htm">
            <item text="Yahoo" url="test.htm"></item>
            <item text="Msn" url="test.htm"></item>
            <item text="Cnn" url="test.htm"></item>
        </item>
        <item text="Mail" url="test.htm">
            <item text="Hotmail" url="test.htm"></item>
            <item text="YahooMail" url="test.htm"></item>
            <item text="OutLookMail" url="test.htm"></item>
        </item>
        <item text="Shipping" url="test.htm">
            <item text="Ups" url="test.htm"></item>
            <item text="Fedex" url="test.htm"></item>
            <item text="Usps" url="test.htm"></item>
        </item>
        <item text="Links" url="test.htm">
            <item text="ASP.NET" url="test.htm"></item>
            <item text="Gotdotnet" url="test.htm"></item>
            <item text="MSDN" url="test.htm"></item>
        </item>
    </item>
    <item text="Project" url="test.htm"></item>
    <item text="Table" url="test.htm"></item>
</menu>

Comments:

zanakomstva s inostrancami
By ? on Friday, May 21, 2010 (PST)
http://needman.ru ????? ?? ?????????? <a href=http://needman.ru>?????????? ? ????????????</a>

Reply to this Comment

heineken mini kegerator
By ? on Friday, May 21, 2010 (PST)
<a href=http://h2o.law.harvard.edu/ViewProject.do?projectID=12410>kegerator</a> http://h2o.law.harvard.edu/ViewProject.do?projectID=12410

Reply to this Comment

pohudenie
By ? on Monday, May 24, 2010 (PST)
<a href=http://acmehitech.com/8/dieta-na-grechki.php>????? ?? ??????</a> http://acmehitech.com//map.html

Reply to this Comment

buy nolvadex low cost
By ? on Thursday, June 03, 2010 (PST)
buy mexican clenbuterol nolvadex online <a href=http://profiles.tigweb.org/buynolvadex>buy nolvadex in the usa</a> buy nolvadex on the net http://profiles.tigweb.org/buynolvadex

Reply to this Comment

nmkbtmklyc
By ? on Sunday, July 04, 2010 (PST)
http://jsnrjhtjerjjghuejrjutgg.com jsnrjhtjerjjghuejrjutgg <a href="http://jsnrjhtjerjjghuejrjutgg.com ">jsnrjhtjerjjghuejrjutgg</a>

Reply to this Comment

hahgfgeghgryhfbwbbtg
By ? on Saturday, July 10, 2010 (PST)
http://hahgfgeghgryhfbwbbtg.com hahgfgeghgryhfbwbbtg <a href="http://hahgfgeghgryhfbwbbtg.com ">hahgfgeghgryhfbwbbtg</a> <a href=http://hahgfgeghgryhfbwbbtg.com>hahgfgeghgryhfbwbbtg</a>

Reply to this Comment

Add Your Comment

Most Popular Downloads

  • Dynamically create TextBoxes and DropDownList with C# and ASP.NET
    This article will show you how to create TextBoxes and DropDownList dynamically on ASP.NET page and then retrieve user input values from dynamically created controls.

  • Dynamically create Controls in ASP.NET with C#
    This article will show you how to create TextBoxes dynamically on web page and then retrieve user input values from dynamically created TextBoxes “Text” property content value.

  • Dynamically create TextBoxes with C# on Windows Form
    Dynamically create and retrieve TextBoxes data with C# on Windows Form

  •  
     
    Home Articles Downloads Resources About