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

.

20 Comments

  1. Aditya says:

    Great article!! Thanks.

  2. [...] for C# using XSD for deserialization U can find the best answer for this question in this site. XML Class Generator for C# using XSD for deserialization | Can Erten See this and tell me how useful was [...]

  3. Robin says:

    Thanks for pointing this out and provide an easy how-to-do-this post ;)

  4. [...] things first.. what is Koen talking about in his post? Well he references another post that describes how you can generates classes out of your XML file. Since all submitted InfoPath [...]

  5. Dragan says:

    Very useful!!! Thanx a lot!

  6. Tom Tom says:

    Sweet man thanks! Extremely useful!

  7. [...] Creating C# classes out of schemas with xsd.exe [...]

  8. Serge says:

    Just saved me A LOT of time. Thank you!

  9. sneh says:

    Thanks a lot..
    I am working on the same kind of project.
    Its about extracting data from database 1, storing those records in xml file , using that xml file to update/add data in database 2 (database 1 and database 2 have same schema. The records are updated in database 1 and then need to be updated in database 2 using this program).In short it is replicating data.

    I am able to generate the xml file and dataset from the data of database1. How should i update/add that data to database2?
    I tried using streamreader and deserialize. i tried using adapters too but it gave error saying adapter.update requires proper insert statement.

    What should my approach be?
    the database is Ms Access and coding in C#.net

  10. Can says:

    Hi sneh,

    Try to generate a dataset instead of a class. The code below uses the class version with the parameter /c.
    Try to do with /d parameter to generate the dataset.
    xsd webSearch.xsd /d /language:vb

    Then load the xml to the dataset by LoadXML method. Then use the database provider and the dataset that you generated, to do update on the database.

    Hope this helps.

  11. sneh says:

    Thanks Can for the quick reply..
    I now have a dataset. Can you please tell me if there is a way to update a database with the dataset i have, without using adapter?

    my flow of the program is : XSD, C# class, XML, Dataset

    I extracted data from database 1 stored in xml file and then from xml file i generated a dataset.
    i now have to use this dataset to update database 2.
    i want to know if there is a way to update database 2 using the dataset without using adapter.

  12. Can says:

    Why don’t you want to use the adapter ? Just set the adapter’s insert update statements and there you go..
    If you don’t want to use adapter, try to create the SQLCommand’s manually with the correct parameters, and set each parameter by reading row by row and executing the sqlcommmand. Well in that case you cango along with the class as well, as you will not really benefit from the dataset.
    Hope this helps.
    Can.

  13. Diggy Zazz says:

    A short and accurate. Game me exactly what I needed. Thanks!

  14. [...] Coding Day » Blog Archive » XML Class Generator for C# using XSD for deserialization 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. Using xsd, part of the .Net framework SDK, this can be done in moments. (tags: xsd xml serialization .net) [...]

  15. DogFace says:

    Uber horny – saved me hours of work!

  16. Faisal Fareed says:

    Exactly what i was looking for… Thanks a million :)

  17. Shujah says:

    Thanks a lot. This really helped me out :)

  18. Koti says:

    Very useful.

  19. Johan says:

    Great article: short simple steps. Saves me lot of time!
    Thx

Leave a Reply