• Using XSD to assist in XML deserialisation to objects in C Sharp
  • In the past when I have performed deserialisation (taking the hierarchical contents of an XML file and converting that that to an object hierarchy at run time) I have used methods such as XmlDocument and manually created objects, or created my own classes and implemented a more traditional style of deserialisation.

    However, there is a much easier way and it can be done in just a few lines of C# code. To do this we need the help of the XML schema definition tool provided by Microsoft. If you have Visual Studio, you will have this tool.

    Firstly, have your XML file handy. In my case, it looked like this:


    To access the schema definition tool (this may vary depending on Windows version) go to the start menu and search for "Command". Find the entry that matches Visual Studio 2008 Command Prompt or similar, depending on the version of Visual Studio installed. Right click it and click Run as Administrator to ensure that there are no permission issues when accessing the XML file.

    Once the command prompt is open, you will need to navigate to the location of your XML file. The method to change directories via command line is slightly different in Windows to Unix if you are changing to another drive. In my case the file was on the E: drive, and to make this the current directory all you need to do is type E: followed by the enter key. From there it is similar to Unix, just type "CD directory_name" to navigate to the desired location.

    Now you will need to type two commands (replacing the filename with your own):

    > xsd yourfile.xml
    > xsd yourfile.xsd /classes

    The command prompt output will look something like this:


    The first command will create an XSD file, which is a schema definition file. If you open it in Visual Studio, it will show you a visual representation of the relationship between the elements in the XML file:


    The second command will create a set of classes output as a .cs file. You don't need to modify that file, all that needs to be done is to drop it into your project. Too easy! You may need to ensure it is namespaced correctly in complex projects, but I found the classes were automatically recognised when I tried this myself.

    Now to access the objects created from your XML file, you need to use code similar to this. You would need to modify the classes to match yours, such as replacing "Entries" with whatever your root node is in the XML file.

    Entries entries;
    string filepath = @"C:\my_xml_file.xml";
    XmlSerializer serial = new XmlSerializer(typeof(Entries));

    using (XmlReader reader = XmlReader.Create(filepath))
    entries = (Entries) serial.Deserialize(reader);

    Once that is implemented, you can manipulate the entries object (or whatever you choose to name it) as you please.

    An example of the object hierarchy after deserialisation can be seen here:


    If you want to modify the Entries object and save the changes back to the XML file that is easily done through a process called serialisation. All you need to do is implement this code (where the entries variable contains the modified Entries object):

    Stream stream = File.Open(filepath, FileMode.Create);
    XmlSerializer saver = new XmlSerializer(typeof(Entries));
    saver.Serialize(stream, entries);

    Using XML files with your .NET application can be very useful, from saving hierarchical data to saving entire application states. Now that you know the basics of serialisation and deserialisation you can make your program perform more cool functions.
    Posted by battye
  • Article search