|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionSuppose 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 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 string s = xmlnode[x].LastChild.InnerText;
You may probably start by debugging to step through and verify 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. BackgroundIn 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 CodeThe 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.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 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:
So, how do you use these examples? Perform the following steps:
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:
Points of InterestWhat 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||