Earlier in the week i posted a Twitter post commenting on how if your not using the XMLSerializer class to you advantage, well, as Scott Hanselman puts it now and then: Your doing it wrong. I use the XML Serializer classes on a daily basis to refer to my XML in a strongly typed manner and often wonder why i see people go to so much effort in creating hard to maintain code just to get data from or send it to an XML file.
Don’t get me wrong, whatever floats your boat. But whenever i curiously ask if they’ve tried to use the XML Serializer classes for such a purpose, they usually state that their is just too much developer “friction” to make their use worthwhile.
This tweaks my interest, as creating an XML class is just way too easy and takes seconds. Not hours. Not Minutes. Seconds. Its just so easy, its almost comical. If this if the first time you’ve done this you’ll be in love – lets take a squiz at it.
What Friction?
So you have an XML file
This is usually where it starts. And this is usually where people lose interest in using the XML Serializer classes. Sure, converting a class you already have created to XML is easy. It looks like below and you’ve probably seen it a million times.
public class Product { public int ProdID; public string ProductName; public static void GenerateXML() { Product p = new Product(); p.ProdID = 10; p.ProductName = "test"; string strMyProductAsXml = Serialize<Product>(p); } public static string Serialize<T>(object objectToSerialize) { //Do cool serialisation here T objOutput = (T)objectToSerialize; XmlSerializer objSerializer = new XmlSerializer(typeof(T)); StringWriter objWriter = new StringWriter(); objSerializer.Serialize(objWriter, objOutput); return objWriter.ToString(); } }
But this is not what we want – we want to do it the other way around. We want to generate the class from the XML not generate the XML from the class.
So lets take a look at our XML:
<?xml version="1.0" encoding="utf-8" ?> <products> <product> <prodID>10</prodID> <prodName>Icecream</prodName> <prodPrice>10.00</prodPrice> </product> <product> <prodID>11</prodID> <prodName>Frosty fruit</prodName> <prodPrice>13.50</prodPrice> </product> </products>
In this user case, we have a bunch of products coming from an API request and i need to use it, so i can DeSerialize it into a class and use it directly in code.
This is where the magical XSD.exe swoops in to save the day. The official name is the XML Schema Definition Tool, and its your new best friend. So lets suit up and hit the road.
So start by saving this XML to a file.
Open up your Visual Studio Command Prompt and move to the directory that your XML file is in.
Type xsd [YOUR XML FILENAME]
This will create a an XSD reference file that replicates your XML files data format.
Now we just need an magical c# class from this – we’re already half way there.
Type xsd [YOUR XSD FILENAME] /c
This will generate a c# class from your XSD file.
And your done!!!
OMG WTF BBQ!!! R U SERIUUZZ??
It’s that simple. Now you have a class named product.cs that will do everything you want plus much much more. Just import it into your project and your done. Did i tell you about the steak knives?
Now you can serialize your xml into this class with ease…
XmlSerializer ser = new XmlSerializer(typeof(products)); StringReader reader = new StringReader(myXmlString); products objOutput = (products)ser.Deserialize(reader);
But what if its not perfect?
There is a chance that it doesn’t work out perfect. Your class may just contain strings, but it needs to have column data types so that you can verify your data and make sure its in the correct format. This is simple. Double click your xsd file and open it in Visual Studio.
In this case, i want to change prodID to an Int32 so that i can make sure i don’t send invalid data to the API I'm planning on using. Select the prodID field and view its properties. Change its data type to Int32 and save your file.
Now open up your Visual Studio Command Prompt windows and run XSD again:
xsd [YOUR XSD FILENAME] /c
Now my product.cs file will make your prodID an integer.
Now go grab a coffee and make a sacrifice to your deity of choice.