Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to create a Data class from an XML document

From Wiki
Jump to: navigation, search
WP Metro Icon File.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
SDK: Windows Phone 7.1
Platform(s): Windows Mango
Windows Phone 8
Windows Phone 7.5
Created: Kavit Patel (26 Mar 2012)
Last edited: hamishwillee (09 Sep 2013)


This article explains how to create a data class from an xml document.


In our Windows Phone application, we need to parse the XML data and fill (deserialize) it into an object. This object will be used to populate the UI and works as our data(model) class.

We can generate the data classes for this object easily using Visual Studio's XSD tool, by following below steps:

  1. Register Microsoft SDK's binary folder (below) path to "Path" in environment variable. (If not done already)
    C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin
  2. Create an XML file from your response string.
  3. Consider the below XML for our example.
  4. Open the command prompt and traverse up to the directory where you saved above XML file.
  5. On the command prompt run the command below
    xsd Students.xml
    (Assuming that the xml file in step 3 has the name Students.xml")
    If we have properly set the environment the above command will be executed with success.
  6. The above command will generate the Students.xsd file in the same folder where our xml file resides. This xsd is a XML schema file and will be used in our next step to generate our C# class file.
  7. Now, on the command prompt run the following command to generate the "Students.cs" file in the same folder where our above two files reside.
    xsd/c Students.xsd

This is our data class which we need to import into our project to deserialize the students XML response.

Check this article where I explain how to deserialize the XML response into an instance of our generated class.

This page was last modified on 9 September 2013, at 06:16.
91 page views in the last 30 days.