Click here to Skip to main content
13,194,662 members (78,659 online)
Click here to Skip to main content
Add your own
alternative version

Stats

15.5K views
205 downloads
10 bookmarked
Posted 12 Apr 2016

SequelMax.NET: C# XML SAX Parser

, 12 Apr 2016
Rate this:
Please Sign up or sign in to vote.
New SAX parsing model comes to .NET!

SequelMax is an improved SAX model where the programmer registers a reading delegate for the XML element of interest. SequelMax.NET engine is ported from the C++ library; The C++ SequelMax is in turn ported and modified from the Portable Elmax DOM engine. Practically, SequelMax.NET uses the same engine. This parsing engine, instead of creating and populating a internal DOM tree, invokes the registered delegates during parsing. A Javascript edition, SequelMax.js, is in the cards.

This is the XML we are going to use for our article example. Notice the last employee does not have a comment.

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee EmployeeID="1286" SupervisorID="666">
        <Name>Amanda Dion</Name>
        <Salary>2200</Salary>
        <Gender>Female</Gender>
        <!--Hardworking employee!-->
    </Employee>
    <Employee EmployeeID="1287" SupervisorID="666">
        <Name>John Smith</Name>
        <Salary>3200</Salary>
        <Gender>Male</Gender>
        <!--Hardly working employee!-->
    </Employee>
    <Employee EmployeeID="1288" SupervisorID="666">
        <Name>Sheldon Cohn</Name>
        <Salary>5600</Salary>
        <Gender>Male</Gender>
    </Employee>
</Employees>

We use the Employee class to hold the data from the XML.

class Employee 
{
    public int EmployeeID;
    public int SupervisorID;
    public string Name;
    public string Gender;
    public double Salary;
    public string Comment;
};

The parsing code to read the employee XML is listed below. To read each element, a delegate has to be registered. A anonymous lambda can be used for delegate. The element path and delegate is stored in a dictionary in the Document class. The parsing engine invokes the delegate whenever the element path is matched. To do the matching, the current element path is generated from a LIFO stack which pushes the element name. After processing, the name is popped. The Open method opens and parses the XML. It is required to set up all the delegates before the Open call.

static bool ReadDoc(string file, List<Employee> list)
{
    SequelMaxNet.Document doc = new SequelMaxNet.Document();

    doc.RegisterStartElementDelegate("Employees|Employee", (elem) =>
    {
        Employee emp = new Employee();
        emp.EmployeeID = elem.Attr("EmployeeID").GetInt32(0);
        emp.SupervisorID = elem.Attr("SupervisorID").GetInt32(0);
        list.Add(emp);
    });
    doc.RegisterEndElementDelegate("Employees|Employee|Name", (text) =>
    {
        list[list.Count - 1].Name = text;
    });
    doc.RegisterEndElementDelegate("Employees|Employee|Gender", (text) =>
    {
        list[list.Count - 1].Gender = text;
    });
    doc.RegisterEndElementDelegate("Employees|Employee|Salary", (text) =>
    {
        Double.TryParse(text, out list[list.Count - 1].Salary);
    });
    doc.RegisterCommentDelegate("Employees|Employee", (text) =>
    {
        list[list.Count - 1].Comment = text;
    });

    return doc.Open(file);
}

The concise code to display the data on the console.

static void DisplayDoc(List<Employee> list)
{
    for (int i = 0; i < list.Count; ++i)
    {
        Console.WriteLine("Name: {0}", list[i].Name);
        Console.WriteLine("EmployeeID: {0}", list[i].EmployeeID);
        Console.WriteLine("SupervisorID: {0}", list[i].SupervisorID);
        Console.WriteLine("Gender: {0}", list[i].Gender);
        Console.WriteLine("Salary: {0}", list[i].Salary);

        if (string.IsNullOrEmpty(list[i].Comment) == false)
            Console.WriteLine("Comment: {0}", list[i].Comment);

        Console.WriteLine();
    }
}

The console output is below. Notice again the last employee does not have a comment.

Name: Amanda Dion
EmployeeID: 1286
SupervisorID: 666
Gender: Female
Salary: 2200
Comment: Hardworking employee!

Name: John Smith
EmployeeID: 1287
SupervisorID: 666
Gender: Male
Salary: 3200
Comment: Hardly working employee!

Name: Sheldon Cohn
EmployeeID: 1288
SupervisorID: 666
Gender: Male
Salary: 5600

Please download the source code at Github.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Cake Processor
Software Developer (Senior)
United States United States

Semi-retired from writing articles but may contribute tips from time to time.



IT Certifications


  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • Certified Secure Software Lifecycle Professional (CSSLP)

You may also be interested in...

Comments and Discussions

 
QuestionHigh Usage? Pin
Sleety8-Apr-14 16:10
memberSleety8-Apr-14 16:10 
AnswerRe: High Usage? Pin
Wong Shao Voon10-Apr-14 20:32
professionalWong Shao Voon10-Apr-14 20:32 
GeneralRe: High Usage? Pin
IgDev16-Apr-14 3:01
memberIgDev16-Apr-14 3:01 
GeneralRe: High Usage? Pin
Wong Shao Voon16-Apr-14 3:51
professionalWong Shao Voon16-Apr-14 3:51 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171018.2 | Last Updated 12 Apr 2016
Article Copyright 2016 by Cake Processor
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid