O/R Mapping with XML

Like XML? Like Objects? Ever wish you would manipulate xml with objects? Read on, will you shortly. Microsoft has a utility – the XML Schema Definition Tool called XSD.exe. It takes a xml flile you make and creates a schema file (a file that describes xml). With that schema file, you create a C# or VB class out of it. Then a few lines of code allow you to easily read in a xml file and automatically map it to an object and vice versa!

Here is a link to the tool

It is a command line tool that works like this. Assuming my xml file is called Data.xml, 2 steps get you a class to use.

Step 1 :XSD Data.xml
Creates data.xsd

Step 2: XSD -c Data.xsd
Creates Data.cs

Step 1 creates the xsd file. Normally you do not have to edit this file. Step 2 then creates a C# class. The -c parameter creates a C# object, I believe -vb creates a Visual Basic object.

Using this class, you can marshal the xml back and forth to objects. Here is some of the code in the example you can download.

XmlDocument xmlData = new XmlDocument();
xmlData.Load(“Data.xml”);
Data objData = (Data)DeserializeObject(xmlData.InnerXml, typeof(Data));

objData.FirstName = “John”;
objData.LastNameName = “Smith”; <- HEY change data using object properties !!!!!

String strData = SerializeObject(typeof(Data), objData);

strData wil be a string of your changed xml.

You load download the source here.

So why do you want to use this. Well for one, the mapping of elements are automatically done for you. You do not have to enumerate through the xml, parse out element, attributes, etc and map them to a class you would have to create. XSD.exe does this for you. XSD.exe is automatically installed for you with Visual Studio. To access it, open a Visual Studio Command Prompt ( this just sets the path for you so the command window can find XSD.exe ). Take a look at the data.cs file it creates. The second and most important reason, you can change data via properties! Just like a good object should.

So from xml to objects back to xml – hmmmm. Life is good.

Now the caveats – This tool takes the lowest common denominator approach. It cant’ all ways figure out all xml so it will default some object in the class as object not as the real class. So you may have to redesign your xml data to use this process but it is well worth it to have the tools do the work. Another huge point, if you decide to add something to your xml or remove it, just rerun the process to build a new class! All done – in a very short amount of time.

This is a very simple example there is a MUCH more you can do with it.

Enjoy….

~ by gordingin on June 15, 2008.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.