Click here to Skip to main content
Click here to Skip to main content

Simple C# XML Parser

, 29 Jan 2007 CPOL
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 PinmemberJamesMMM18-Sep-07 7:10 
GeneralRe: I'm stuck on reading the return values from Array attribs Pinmemberm1chu_29-Mar-09 1:13 
GeneralRe: I'm stuck on reading the return values from Array attribs PinmemberGSGeek26-May-09 18:14 
Newsupdated PinmemberBizzyD29-Jan-07 6:44 
QuestionHow to use it? PinmemberTauZod22-Nov-06 5:35 
AnswerRe: How to use it? PinmemberKingLo0oza3-Mar-07 9:06 
GeneralNice try but PinmemberEducatedMonkey21-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
Web02 | 2.8.141216.1 | Last Updated 29 Jan 2007
Article Copyright 2006 by BizzyD
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid