Xml To Xml Conversion

We all know you can do an xml to html conversion. Did you know you can do an xml to xml conversion also?
Why?
If you ever get data as xml, but you cannot control the formatting, you can alter it into another xml document.
Why?
One way to use this is to think about a DataSet object. (You’re using strongly typed datasets and not loosely typed datasets, right?)
If you remember that a DataSet is just some fancied up Xml, you can convert your original xml into one that matches your dataset schema.

Create a new strongly typed dataset. Call it MyStronglyTypedDS.
Add an "Author" table.
Add elements : MyUniqueID, LastName, FirstName, TheBookCount. ( string, string, string, int )
Save your file.

Now load the example which you can download here.

There are 3 files. myxml.xml, myxsl.xsl, runit.vbs. (The vbs is to keep it simple, but you can easily find the DotNet code to do this).

Run runit.vbs. It will create a new file called results.xml.

Voila! An xml to xml conversion, which outputs to the matching schema of the (strong) dataset you created above.

MyStronglyTypedDS ds = new MyStronglyTypedDS();
MyStronglyTypedDS.ReadXml(@"c:\mypath\results.xml");

Or check this MS KB article:
http://support.microsoft.com/kb/311566

The original need I had for this method was that I was getting the same data from customers, however, each customer sent the data just a little differently. They were actually sending excel files. I saved off the excel (as xml) programatically. Then I had one xsl file per customer, that translated the data into a common xml schema. And Boom! When a new customer had a new and different format, all I had to do was write 1 xsl file, and I could use their data, without altering any of my original code (It’s kind of an OO concept of Open-Closed-Principal (OCP), but with xsl)  (<< How’s that for a stretch?). Writing an xsl file for excel/xml is a little trickier, but can be done. See a previous post from me and you can see some of that syntax and namespace issues.

Advertisements
This entry was posted in Software Development. Bookmark the permalink.

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 )

Google+ photo

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

Connecting to %s