How to create a xml file and .NET Object Class from their xsd

debal_saha-9451
Posted by in XML category on for Intermediate level | Points: 250 | Views : 53524 red flag
Rating: 5 out of 5  
 1 vote(s)

Hello friends, recently I stuck with this issue and find the answer in StackFlowOver. Though there is some third party tools are available to automatically do that.I’m also a beginner in XML .In my this simple article I want to describe step by step how can we create a xml document from their schema. But before that let’s know the basics things. I know everyone know about xml except me. So, let’s revise in this.

What is XML?

XML Stands for Extensible Markup Language which is much more like HTML is designed for carry data but not to display the data, improves web functionality by identifying your information in a more flexible, accurate and adaptable way . It’s a self describe language.
Remember though I tell that xml is much more similar HTML but there is great difference.  XML specially designed for Store and Transport data on basis of what data is. But HTML basically designed for displaying data , how its look .
Other Different between Xml and HTML is in Xml there is no predefined Tags, but in HTML it has like <h1></h1>, <P></P> etc….

What is XML Schema?

An XML Schema is described the structure of an XML Document. XMl Schema defines the elements, attribute that can appear in document. This Document also can define the order, data type of Child elements and attribute, numbers of child elements and also can define whether a child element is empty or can include text.

What is DTD?

Data Type Document is an XML/HTML document which provides a list of the elements, attributes, Comments, notes, entities in the document. It can also indicate their relationship to one another in the document, So, it can say that DTD is a grammar of XML or HTML Document.

The main Purpose of using DTD for XML or XHTML, is to give the structure for your documents. It is very easy to write an XML documents , but using DTD it will be a syntactic meaning to the Computer .

So, what is difference between XML Schema/XSD and DTD?

An Xml Schema Definition (XSD) is a current standard schema language for all XML documents and data. World Wide Web Consortium(W3C) published in its version 1.0 format , on 2nd  May 2001 . Actually it is none other but Previous defined XMl Schema. Modern XSD is based on W3C 2001 recommendation.

Lets see the what’s the difference between Modern XSD and their Previous DTD(Data Type Document). XSD has improved the limitation of the DTD .

XSD DTD
XSD is modern standard Schema languages of  XML documentation. DTD is Data Type Document which is older way to describe the structure and content of  XML Document.
XML Schema Definition is namespace awareness. DTD has no namespace awareness.
XMl Schema Definition is only written in XML languages. So no need to learn other languages. DTD can be written in HTML,XML,XHTML etc.
Another great advantage of XML Schema is to give the ability to implement strong typing . It can define the datatype of certain elements , constrains to it with specific length or value. DTD lacks of Strong type typing and has no way of validating the content to data type.
XMl Schema doesn’t allow inline definition. While DTD allows you inline definition.

So, this is  small basic theory. Now we shall start to do work in it . Everything I have done in Visual Studio 2011.

1. Open Visual Studio 2011 . Click on new Project. Select the Console Application . Give your proper file path and Press ok.

2. Now I have downloaded a sample XSD form internet, you can get it in Microsoft Site also. Save it in Desktop. Now need to add this in your project.

3. Copy the File from your Desktop and now select the Project in Solution Explorer, right click on it and click to Paste it.

4. So, your File has been added in your solution. Double Click in it , See the below image .

Pic -1

5. Click on the Area as I marked with red color. You will able to see your xsd file. Here is the below my xsd file in Visual Studio 2010.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org" targetNamespace="http://tempuri.org" elementFormDefault="qualified">

<xsd:element name="PurchaseOrder" type="tns:PurchaseOrderType"/>

<xsd:complexType name="PurchaseOrderType">

<xsd:sequence>

<xsd:element name="ShipTo" type="tns:USAddress" maxOccurs="2"/>

<xsd:element name="BillTo" type="tns:USAddress"/>

</xsd:sequence>

<xsd:attribute name="OrderDate" type="xsd:date"/>

</xsd:complexType>

<xsd:complexType name="USAddress">

<xsd:sequence>

<xsd:element name="name" type="xsd:string"/>

<xsd:element name="street" type="xsd:string"/>

<xsd:element name="city" type="xsd:string"/>

<xsd:element name="state" type="xsd:string"/>

<xsd:element name="zip" type="xsd:integer"/>

</xsd:sequence>

<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>

</xsd:complexType>

</xsd:schema>

6. So this is our xsd file so, here we see that the schema represent the xml document.  Now we shall look the view of Schema in Visual Studio.  How? Ok See the below Pic – 2. How to see the XML Schema Explorer. Just go to the View Tab of the Visual Studio IDE and Click on the Xml Schema Solution Explorer.

7. So, if you look at the above Pic -2 view of 1stschema. xsd , you see the green color along with <> icon. So what does this means? Yes, u r right? ? these are represent as a XML Element. And @ symbol represent as attribute.

8. Ok, Now we should focus on our target that how to generate sample xml documents from this xsd view .  Ok, fine .This is two step work .  Just  see in the schema explorer as  I showed in Pic -2 and Click on the root node and choose  to “generate Sample XML “. See in Pic -3 .

9. Excellent, You can see your xml document is ready. Here is the below xml. Look at it.

<?xml version="1.0" encoding="utf-8"?>

<PurchaseOrder OrderDate="1900-01-01" xmlns="http://tempuri.org">

<ShipTo country="US">

<name>name1</name>

<street>street1</street>

<city>city1</city>

<state>state1</state>

<zip>1</zip>

</ShipTo>

<ShipTo country="US">

<name>name2</name>

<street>street2</street>

<city>city2</city>

<state>state2</state>

<zip>-79228162514264337593543950335</zip>

</ShipTo>

<BillTo country="US">

<name>name1</name>

<street>street1</street>

<city>city1</city>

<state>state1</state>

<zip>1</zip>

</BillTo>

</PurchaseOrder>

10.So , our one task is complete . Now we need to create .NET Class in  C# language from xsd file . So , how to do that ? Ok . For this we use xsd.exe tool. Now you can ask what is this ? Ok there is a good description about it in MSDN. In One Words , we can say that xsd.exe is a xml schema definition tool which generate xml schema or Common Language runtime classes from XDR, XML and XSD Files. Interesting ? Fine , now we implement this .

11. For Better result what I have done first is Click on the save icon in Visual Studio, So that our created all files will be saved .  Now Click on Open File Icon and go to in your project folder where your all files are going to save  or this can also be done , just select your project in Solution Explorer, right click on it and go to “Open Folder in Window Explorer”. Now there you will find your xsd file . You know my xsd file name is 1stSchema.xsd. So, what I do , just Ctrl + C to Copy the File and make a new folder in my C : drive  give the folder name Debal and Ctrl + V to paste the file . you can choose any drive as per your requirement . Just see below Pic -4 .



 Pic – 4.

12. This is time to do work in Visual Studio Command Prompt . Not able to find this . Ok in your computer go to All Program , you can find this after click on the Start Button. Find out Microsoft Visual Studio 2010 Node , Click it when it expand then click on Visual Studio Tools . There you will find Visual Studio Command Prompt(2010) . See the Pic -5.

13. Our Command Prompt is ready to do work . Have a look on that also. Pic -6.

Pic -6

14. Now we need to write command in your black window.  You can see that something written in our Command Prompt, Actual is showing the Path of the Visual Studio where you were choosing the Path at time of installing Visual Studio. In my computer it is in E: Drive.  Our main task using this Command Prompt is to Create .NET C# Classes using xsd file. As I described in previous step to copy 1stSchema.xsd to your favorable location . I save it in D: drive . See in Pic – 4.

15. Now in my Command Prompt I need to move in C : drive from E: Drive . So, using the command prompt write this first CD C: and Press Enter and then also write C: and Press Enter . Now need to give the folder name , so this can write as CD Debal and Press Enter .

16.Now need to give the path of our 1stSchema.xsd file . Now we give the command to create Class.  This can be written as like this:

Xsd  /c 1stschema.xsd and press Enter .

Look at the below Image.

Pic – 6
17.So , Your Classes are ready for you see in your folder where you save the xsd file . In my case this is written in my C : -> Debal Folder.



Pic – 7
18. So, Now you can Copy this Class and Paste it in Your Project , Just Select the Project in your Visual Studio Solution Explorer and right click on it , then click Paste . See in Pic - 8

Pic – 8
19. So, You see how we easily can create a class from XML Schema Using Xsd.Exe Tools. And also Can create xml document form their schema .

If anything you want to share about this topic , write your  response to improve this article . Thank You.

Page copy protected against web site content infringement by Copyscape

About the Author

debal_saha-9451
Full Name: DEBAL SAHA
Member Level: Starter
Member Status: Member
Member Since: 3/15/2011 4:01:43 AM
Country: India

http://silverlightpractice.blogspot.com/

Login to vote for this post.

Comments or Responses

Posted by: Learningtorise on: 1/14/2014 | Points: 25
Nice post, dude. Keep up the good work!
Posted by: Santoshi14 on: 11/20/2014 | Points: 25
Very nice post. Thank you

Login to post response

Comment using Facebook(Author doesn't get notification)