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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • IT decision support impacts all aspects of technology management, from governance and strategy to budgets and resource planning. IT decision support effectiveness often falls prey to data-driven challenges that make it difficult to understand the data in context. These challenges: overwhelming data volumes, heterogeneous data types, and growing data complexity. This Forrester Consulting Paper reports the three key findings from their study conducted, on behalf of BDNA, to test the hypothesis that data …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds