×
Namespaces

Variants
Actions

How to create a Data class from an XML document

From Nokia Developer 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
Compatibility
Platform(s): Windows Mango
Windows Phone 8
Windows Phone 7.5
Article
Created: Kavit Patel (26 Mar 2012)
Last edited: hamishwillee (09 Sep 2013)

Introduction

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


Implementation

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.
    XMLResponse.png
  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
    XSDExample.jpg

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.
106 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×