HowTo: Create a Data class from an XML document
See Also
Article Metadata
Tested with
SDK: Windows Phone 7.1
Compatibility
Platform(s): Windows Mango
Article
Created: Kavit Patel
(26 Mar 2012)
Last edited: hamishwillee
(30 Nov 2012)
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 (serialize) them to a class. This class will be used to populate the UI and works as our data(model) class.
We can generate our data classes easily using Visual Studio's XSD tool, by following below steps:
- 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
-
- Create an XML file from your response string.
- Consider the below XML for our example.
- Open the command prompt and traverse up to directory where you saved above XML file.
- On command prompt run the below command
-
xsd Students.xml
- (Assuming that xml file in step 3 has name Students.xml")
- If we have properly set the environment the above command will be executed with success.
-
- The above command will generate the Students.xsd file in same folder where our xml file resides. This xsd is the XML schema file and will be used in our next step to generate our C# class file.
- Now, on command prompt run the following command to generate the "Students.cs" file in same folder where our above two files reside.
This is the our data class which we need to import in our project to de-serialize the students XML response.
Check this article where I explain how to de-serialize the XML response in data class's object.



Hamishwillee - Where is the next article
Hi Kiran
You write " will explain in my next article about how to de-serialize the response in data class's object." When is this planned, and if you are going to write it, can you link it? If not, please tidy this to a good state?
For my interest, why might you do this? I think what you're saying is that this will create a class object that you can use to parse this sort of XML into your UI. Is there no way to do this directly in XML rather than via another class you have to write. Is this similar in some way to the fact that you can't use SQL direclty, but have to write classes to access tables in your database?
Could you please also add other good references in this topic on MSDN etc?
Regards
Hhamishwillee 10:41, 25 April 2012 (EEST)
Kavit Patel - Re: Hamishwillee - Where is the next article
Hi Hamishwillee,
I have created next article, please visit http://www.developer.nokia.com/Community/Wiki/Deserialize_the_XML_response_to_a_Data_class
Also, Yes. We can directly parse the XML without having class. But this is the simplest way to have your data parsed. You don't need to even care about parsing the responses. Another thing is that it is better to have a data (model) class for your responses (As part of MVC or MVVM architecture), you treat your XML data classes same like other classes in your application, thus easily get the values to show in UI.
This is useful mainly if you are requesting to PHP or other servers rather than .NET, if it is .NET server then it is better and easier to add the service reference rather than doing all these stuffs.
If you look at the wiki page there are Employees & Employee Classes, which are created manually to use with XmlSerializer. If we make use of "XSD" tool then we can have those two classes created automatically by the tool and we can use these classes in same manner like classes created manually, for parsing purpose.
I searched for some references for you and found below two some useful.
References: 1) http://msdn.microsoft.com/en-us/library/x6c1kb0s%28v=VS.71%29.aspx 2) http://www.codeproject.com/Articles/12886/Simplifying-Serialization-and-De-serialization-Pro
Regards,
Kavit.Kavit Patel 15:41, 25 April 2012 (EEST)
Hamishwillee - Thanks for the explanation
Hi Kavit
Thanks for the explanation.
I believe I understand what is going on now. You're creating class deserialise specific XML - and what this article shows is a way of creating the class using a tool rather than having to manually create it. Coming from the Qt world this looks a bit clunky compared to being able to automatically parse arbitrary parameters into a model using xpath. I guess though it is more efficient than having to look at parser output directly.
From a documentation point of view, the problem is that this is not explained - you need to add the explanation up the top in the introduction. If it were me I would also combine the two documents under a single one with title "Deserialising an XML document using a data class". Do you want me to do this merge for you?
Regards
Hhamishwillee 05:17, 26 April 2012 (EEST)