Click here to Skip to main content
11,490,007 members (63,006 online)
Click here to Skip to main content

Simple C# XML Parser

, 29 Jan 2007 CPOL 42.3K 473 16
Rate this:
Please Sign up or sign in to vote.
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.

XmlParser xp = new XmlParser();

Next, start to fill in the events.

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:

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:

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 
GeneralRe: I'm stuck on reading the return values from Array attribs Pin
m1chu_29-Mar-09 1:13
memberm1chu_29-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    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 29 Jan 2007
Article Copyright 2006 by BizzyD
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid