Click here to Skip to main content
12,695,743 members (31,117 online)
Click here to Skip to main content
Add your own
alternative version

Stats

187.3K views
2.7K downloads
36 bookmarked
Posted

XML parsing in C#

, 29 May 2002
Rate this:
Please Sign up or sign in to vote.
This project gives you a head start to write an XML parser in C#.
<!-- Download Links --> <!-- Add the rest of your HTML here -->

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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionNot working Pin
Ljupco Sofijanov23-Jun-11 1:50
memberLjupco Sofijanov23-Jun-11 1:50 
GeneralHashtable not available Pin
Member 789702911-May-11 4:41
memberMember 789702911-May-11 4:41 
GeneralRe: Hashtable not available Pin
Liaqat Fayyaz3-Sep-11 10:26
memberLiaqat Fayyaz3-Sep-11 10:26 
GeneralBug Pin
Nanodeath13-Jul-07 11:16
memberNanodeath13-Jul-07 11:16 
GeneralTypo Pin
Anonymous31-Mar-03 15:24
sussAnonymous31-Mar-03 15:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 30 May 2002
Article Copyright 2002 by Sanjay Ahuja
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid