Subscribe to Feed

14Dec

How to generate C# Class files (Objects) from XML file?

Posted by admin as .Net, C#, Microsoft, Visual Studio

 

You may run into a situation on several occasions where in you have to generate a object file (like .cs file) from a XML file. For example, you may have a XML/SOAP response from a web service that need to be de-serialized and consumed in your application etc..

Luckily because of the tools shipped with Visual Studio this is not that of a difficult task. Here are the steps that will help you to deserialize the XML file and generate an object file. I assume that you have the XML file (Test.xml) on hand and have Visual Studio installed on the machine.

  • Open Visual Studio Command Prompt by browsing to and launching  Start -> All Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio 2010 Command Prompt

Note: If you have another verson of Visual Studio installed replace 2010 in the above location with your version of Visual Studio. For example if you have VS 2008 installed, you should launch Start -> All Programs -> Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 Command Prompt

  • After launching the VS command prompt, run the following command to generate an XSD out of the xml file. (You can replace the C:\Temp with your preferred location. XSD.exe will dump the XSD file(s) in the given directory.)

    xsd.exe test.xml /outputdir:C:\Temp
  • If the XSD generates additional XSD files (like TestApp.xsd etc,.) ignore those files.
  • Now, lets generate the class file with the generated XSD. We use the XSD.exe again to generate the object file for us. Replace the MyNamaspace below with the namespace your object file (.cs file) should be included in.
    xsd.exe C:\Temp\Test.xsd /c /n:MyNamespace
  • Running the above command will generate the .cs object file you needed, but we need to make couple of additional changes to the generated file to make it work as expected for us. To start with, open the generated .cs file, and scroll to the end and delete any partial classes generated for the XSDAPP file generated above.
  • Then scroll back to the top to the root item, and change the multidimensional item array to single dimension.  Change both the property and private item array to single dimension.
    // Original
    private yourItem[][] objectName;
    
    public yourItem[][] objects
    {
            get{...}
            set{...}
    }
    
    // After Changing
    private yourItem[[] objectName;
    
    public yourItem[] objects
    {
            get{...}
            set{...}
    }
  • Now, all that is left is matching the required collections to the XML items. For example lets assume that the above property maps to a results collection which has several results, i.e XML file looks something like 
    <results>
       <item>..</item>
       <item>..</item>
       <item>..</item>
        ...
        ...
    </results>

    You want add the corresponding xml attributes to the above property. So, now the above property looks some thing similar to

    // After Changing
    private yourItem[[] objectName;
    
    [XmlArray("results")]
    [XmlArrayItem("item", typeof(yourItem))]
    public yourItem[] objects
    {
            get{...}
            set{...}
    }
     

That’s all folks, this should get you the class file you need. All you have to do add an Existing item to your project and select the above generated Class file.

    Hope this helps,

    CTRL+F5

Related Headings:

  • How to De-serialize an XML file in C#?
  • How to generate an object file from XML in .Net?

Comment Form