Click here to Skip to main content
Licence CPOL
First Posted 25 Sep 2006
Views 32,692
Downloads 148
Bookmarked 16 times

Simple C# XML Parser

By | 29 Jan 2007 | Article
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)

About the Author

BizzyD



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralI'm stuck on reading the return values from Array attribs PinmemberJamesMMM6:10 18 Sep '07  
GeneralRe: I'm stuck on reading the return values from Array attribs Pinmemberm1chu_0:13 29 Mar '09  
GeneralRe: I'm stuck on reading the return values from Array attribs PinmemberGSGeek17:14 26 May '09  
Newsupdated PinmemberBizzyD5:44 29 Jan '07  
QuestionHow to use it? PinmemberTauZod4:35 22 Nov '06  
AnswerRe: How to use it? PinmemberKingLo0oza8:06 3 Mar '07  
GeneralNice try but PinmemberEducatedMonkey12:38 21 Oct '06  

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.

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