Writing XML SAX Parsers in C#

Environment: C#

This project gives you a head start on writing XML parsers in C#. The important namespace to achieve our goal would be System.Xml. Though you might find it strange, unlike other languages/class libraries, there are no direct functions such as startElement and endElement at your disposal, or to come to your rescue. You will have to manage this on your own. So, let's look at this important block of 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 you can 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 by using any of the read methods and properties that 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.



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds