How to create a Data class from an XML document
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:
- 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 the directory where you saved above XML file.
- On the command prompt run the command below
- (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.
- 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.
- 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.
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.