Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#

Simple C# XML Parser

Rate me:
Please Sign up or sign in to vote.
2.00/5 (6 votes)
29 Jan 2007CPOL 62.6K   734   16   7
Simple XML parser for C# for those who are used to the ways of libxml2 or Expat.

Introduction

This is a very simple XML parser written for C# for people who are used to using the callbacks of libxml2 and Expat, such as: OnElementStart, OnElementEnd, and OnData.

It is quite simple to use as it uses delegates and events.

Start off by adding the XmlParser.cs file to your project and creating an XmlParser object.

C#
XmlParser xp = new XmlParser();

Next, start to fill in the events.

C#
xp.OnElementStart += new OnElementStartD(xp_OnElementStart);
xp.OnElementEnd += new OnElementEndD(xp_OnElementEnd);
xp.OnElementData += new OnElementDataD(xp_OnElementData);

VC# will do most of the work for you with tab complete; if it doesn't, create the following functions:

C#
void xp_OnElementData(string name, string CData)
{
    throw new Exception("The method or operation is not implemented.");
}

void xp_OnElementEnd(string name)
{
    throw new Exception("The method or operation is not implemented.");
}

void xp_OnElementStart(string name, string ns, int numAttribs, Array attribs)
{
    throw new Exception("The method or operation is not implemented.");
}

Then, replace the exceptions with whatever code you want to, to parse the XML code.

Lastly, under where you filled in the events, do something like:

C#
xp.Load(myXmlFile);
xp.Parse();

Happy XML parsing!

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI'm stuck on reading the return values from Array attribs Pin
JamesMMM18-Sep-07 6:10
JamesMMM18-Sep-07 6:10 
How do i read the array of structs from my calling program?

void xp_OnElementStart(string name, string ns, int numAttribs, Array attribs)
{
Console.WriteLine(attribs.GetValue(i).attrName); ?????
}

Can't seem to get the format correct to read the values from attribs, e'g',
attrName and attrVal;

Please help
GeneralRe: I'm stuck on reading the return values from Array attribs Pin
m1chu.eu29-Mar-09 0:13
m1chu.eu29-Mar-09 0:13 
GeneralRe: I'm stuck on reading the return values from Array attribs Pin
GSGeek26-May-09 17:14
GSGeek26-May-09 17:14 
Newsupdated Pin
BizzyD29-Jan-07 5:44
BizzyD29-Jan-07 5:44 
QuestionHow to use it? Pin
TauZod22-Nov-06 4:35
TauZod22-Nov-06 4:35 
AnswerRe: How to use it? Pin
KingLo0oza3-Mar-07 8:06
KingLo0oza3-Mar-07 8:06 
GeneralNice try but Pin
EducatedMonkey21-Oct-06 12:38
EducatedMonkey21-Oct-06 12:38 

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.