Click here to Skip to main content
15,883,647 members
Articles / Programming Languages / XML
Article

Easy XML Parsing in C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (14 votes)
2 Mar 2008LGPL33 min read 173.2K   5.9K   61   7
The easiest way to get XML parsing in C#

Introduction

Suppose you have an XML Schema with XML files that satisfy it and you need to get data out of the XML files in a way that is easy to read and maintain. There are quite a few ways to approach this problem in any language (C# included). The XmlSerializer is the one stop shop for quick and easy XML parsing in C#. The purpose of this article it to provide an overview to generating C# object bindings for a schema and parsing an XML file using those bindings. This is accomplished by demonstrating this practice using two different sets of XML Schemas and XML files.

Why Not Use XmlNode / XMLDocument?

The readability and maintainability of your XML parsing code can be improved using the methods outlined in this article. Say you were given a baseline of code and were told that it was receiving a NullReferenceException on the following line:

C#
string s = xmlnode[x].LastChild.InnerText; 

You may probably start by debugging to step through and verify xmlnode, xmlnode[x] and LastChild were valid. You may find out that your XML file does not contain a LastChild for a given xmlnode[x]. You will need to dig through the schema manually, verify what the xmlnode[x]s are all about and start playing XML / code detective. The problem here is, from the code itself, you have no real guidance as to what structure you are dealing with. Now, suppose you were given the following line:

C#
string s = shiporder.Items[1].quantity; 

This line gives you more insight into what your code is meaning to do. In addition, it provides the reader with a definition of the structures these operations will be performed on; no detective work required.

Background

In order to use the following samples, you will need to have a working copy of Visual Studio 2005 or 2008. If you are unfamiliar with the xsd.exe utility distributed with these environments, I would recommend running a /? at the command prompt to get familiar with it. In addition, a basic understanding of XML and XML Schema documents is always handy. The two XML schemas used for this article were obtained from here.

Using the Code

The code provided in csharpxmlparsing.zip contains two example schemas, XML files and test code. Two examples provided demonstrate how different schemas look and feel when writing the parsing code. Here's a rundown on the contents.

The following files relate to the notes example:

notes.xml     - notes data file
notes.xsd     - notes schema 
notesmain.cs  - What you would have to write to parse notes XML files

The following files relate to the shiporder example:

shipmain.cs   - What you would have to write to parse shiporder xml files
shiporder.xml - shiporder data file
shiporder.xsd - shiporder schema

The two files that represent what you will need to write if you would need to parse the XML can be found in notesmain.cs and shipmain.cs. Notesmain.cs has been provided here to give you a reasonably good example of what the parsing code looks and feels like:

snippet.jpg

So, how do you use these examples? Perform the following steps:

  1. Unzip the csharpxmlparsing.zip file to a desired location
  2. Start up a Visual Studio 2005/2008 Command Prompt
  3. cd to the path (location) where you uncompressed csharpxmlparsing.zip
  4. Run the file named go.bat

This step will produce two new files named notes.cs and shiporder.cs. These are the C# classes that are used to represent the structure of the provided schemas. These files should never be modified by hand. In addition, two executable files will be produced, named: notes.exe and shipmain.exe. After being built, these two executable files are then executed, so their output can be displayed. The output should look similar to that found below:

godotbat.jpg

Points of Interest

What happens if your schema changes? Using this method, simply regenerate your object bindings using the xsd.exe utility and modify your code to support old structures that have changed or new structures introduced.

History

  • 2nd March, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
United States United States
I love to write programs and vim is my editor of choice. Programming languages fascinate me and I feel at least reasonably familiar with C, python, C++, JAVA, C#, ECMAScript, Lua and Ruby. I also use a few open source relational database systems; postgresql, mysql, sqlite. Some of my interests include: programming languages, geoprocessing, number theory, network protocols, web services, cryptography, linux, windows, open source, GIS.

See: http://www.joevial.com

Comments and Discussions

 
GeneralThe first time I built files in cmd way Pin
leiyang-ge17-Mar-13 16:26
leiyang-ge17-Mar-13 16:26 
GeneralMy vote of 4 Pin
Vyas Darshan3-Jan-13 2:01
Vyas Darshan3-Jan-13 2:01 
QuestionHow to load the XmlSerializer from a string Pin
dennisk6822-May-08 2:45
dennisk6822-May-08 2:45 
AnswerRe: How to load the XmlSerializer from a string Pin
Joseph Armbruster9-Dec-08 10:47
Joseph Armbruster9-Dec-08 10:47 
GeneralGenerate object from XML Pin
Harry Chou5-Mar-08 4:33
Harry Chou5-Mar-08 4:33 
GeneralRe: Generate object from XML Pin
Joseph Armbruster11-Mar-08 2:55
Joseph Armbruster11-Mar-08 2:55 
GeneralVTD-XML Pin
Jimmy Zhang2-Mar-08 12:22
Jimmy Zhang2-Mar-08 12:22 

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.