XML Class Generator for C# using XSD for deserialization

Let's say we have an XML file and we want to deserialize that file to our implemented class. This is an easy task if the XML file is simple. However if it has more complex types, it can take a long time to implement the class without error. XSD comes with .Net framework SDK. I does not have a user interface, we can access it from the command line tools.
  1. We start it from Visual Studio 2005 -> Visual Studio Tools -> Visual Studio Command Prompt .
  2. Next we need to have a valid XML file that I want to generate the class from. I just use for this sample, the xml output of the yahoo search REST query. Just dowload the xml output of the query Yahoo Search xml+class+generator or any other xml file that you want to generate the class from. We save the file as xml.
  3. We use the command xsd to the xml file to generate the xsd schema file.
  4. D:\\test>xsd webSearch.xml Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 2.0.50727.42] Copyright (C) Microsoft Corporation. All rights reserved. Writing file 'D:\\test\\webSearch.xsd'. D:\\test>

  5. Next we use the generated xsd file to generate our class. The generated xsd can contain multiple class, so it would be better to use /classes switch. The default language is C#; however you might want to use it in your Visual Basic Project, to do that just add the switch /language:vb
  6. D:\\test>xsd webSearch.xsd /CLASSES /language:vb Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 2.0.50727.42] Copyright (C) Microsoft Corporation. All rights reserved. Writing file 'D:\\test\\webSearch.vb'. D:\\test>xsd webSearch.xsd /CLASSES Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 2.0.50727.42] Copyright (C) Microsoft Corporation. All rights reserved. Writing file 'D:\\test\\webSearch.cs'.

  7. Now we have the class file that we use to deserialize the xml object without any exception. So I add the XML file and the generated cs file to my project.
  8. using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace XSDTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.IO.StreamReader str = new System.IO.StreamReader("webSearch.xml");
                System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ResultSet));
                ResultSet res = (ResultSet) xSerializer.Deserialize(str);
                foreach (ResultSetResult r in res.Result)
                {
                    Console.WriteLine(r.Title);
                    Console.WriteLine(r.Summary);
                    Console.WriteLine();
                }
                str.Close();
                
                Console.ReadLine();
            }
        }
    }
  9. Here is the output :
  10. XML Class Generator for C++ Oracle9i XML Developer's Kits Guide - XDK. Release 2 (9.2) Part Number A96621-01 . 19. XML Class Generator for C++ This chapter contains the following sections: Accessing XML C++ Class Generator ... Accessing XML C++ Class Generator. The XML C++ Class Generator is provided with Oracle9i and is also available for XML Class Generator for Java Oracle9i XML Developer's Kits Guide - XDK. Release 2 (9.2) Part Number A96621-01 . 7. XML Class Generator for Java. This chapter contains the following sections: Accessing XML Class Generator for Java ... The Oracle XML Class Generator for Java is provided with Oracle9i's XDK for Java ...

As you may see this is the easiest and exceptionless solution for using xml output of some web services. What we simply do is generate the classfile, deserialize the xml file to our class and use it.

Download

XML C# deserialization Project.