Click here to Skip to main content
15,121,030 members
Articles / Programming Languages / C#
Article
Posted 25 Sep 2006

Stats

60.6K views
707 downloads
16 bookmarked

Simple C# XML Parser

Rate me:
Please Sign up or sign in to vote.
2.00/5 (6 votes)
29 Jan 2007CPOL
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)

Share

About the Author

BizzyD
United States United States
No Biography provided

Comments and Discussions

 
GeneralI'm stuck on reading the return values from Array attribs Pin
JamesMMM18-Sep-07 7:10
MemberJamesMMM18-Sep-07 7: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 1:13
Memberm1chu.eu29-Mar-09 1:13 
GeneralRe: I'm stuck on reading the return values from Array attribs Pin
GSGeek26-May-09 18:14
MemberGSGeek26-May-09 18:14 
Newsupdated Pin
BizzyD29-Jan-07 6:44
MemberBizzyD29-Jan-07 6:44 
QuestionHow to use it? Pin
TauZod22-Nov-06 5:35
MemberTauZod22-Nov-06 5:35 
AnswerRe: How to use it? Pin
KingLo0oza3-Mar-07 9:06
MemberKingLo0oza3-Mar-07 9:06 
GeneralNice try but Pin
EducatedMonkey21-Oct-06 13:38
MemberEducatedMonkey21-Oct-06 13: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.