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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds