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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds