5,691,626 members and growing! (13,348 online)
Email Password   helpLost your password?
Languages » C# » How To     Intermediate

XML parsing in C#

By Sanjay Ahuja

This project gives you a head start to write an XML parser in C#.
C#, Windows, .NET 1.0, .NETVisual Studio, VS.NET2002, Dev

Posted: 29 May 2002
Updated: 29 May 2002
Views: 102,413
Bookmarked: 26 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
33 votes for this Article.
Popularity: 2.68 Rating: 1.76 out of 5
17 votes, 58.6%
1
3 votes, 10.3%
2
2 votes, 6.9%
3
3 votes, 10.3%
4
4 votes, 13.8%
5

Introduction

This project gives you a head start to write an XML parser in C#. The important namespace to achieve our goal is System.Xml. Though you might find it strange, unlike other languages/class libraries their are no direct functions like startElement and endElement to your rescue. You will have to manage all this on your own. So let's start looking at this important block of the code

void ParseURL(string strUrl)
{
    try
    {
        XmlTextReader reader = new XmlTextReader(strUrl);
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
            case XmlNodeType.Element:
                Hashtable attributes = new Hashtable();
                string strURI= reader.NamespaceURI;
                string strName= reader.Name;
                if (reader.HasAttributes)
                {
                    for (int i = 0; i < reader.AttributeCount; i++)
                    {
                        reader.MoveToAttribute(i);
                        attributes.Add(reader.Name,reader.Value);
                    }
                }
                StartElement(strURI,strName,strName,attributes);
            break;
             //

             //you can handle other cases here

             //

 
             //case XmlNodeType.EndElement:

             // Todo

             //case XmlNodeType.Text:

             // Todo

            default:
            break;
        }
    }
    catch (XmlException e)
    {
        Console.WriteLine("error occured: " + e.Message);
    }
}

As we see the main class here is XmlTextReader. XmlTextReader provides forward-only, read-only access to a stream of XML data. The current node refers to the node on which the reader is positioned. The reader is advanced using any of the read methods and properties reflect the value of the current node. Note that we cache the element name before we move to the attributes.

The NodeType property of XmlTextReader gets the type of the current node. Accordingly we process the node and call necessary functions on it.

You can forward me your comments at lparam@hotmail.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sanjay Ahuja


Sanjay Ahuja is a Bachelor of Engineer and has completed his CDAC in Pune,India. He is currently working as a consultant for Verizon.
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralBugmemberNanodeath11:16 13 Jul '07  
GeneralTyposussAnonymous15:24 31 Mar '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 May 2002
Editor: Chris Maunder
Copyright 2002 by Sanjay Ahuja
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project