Monday, April 14, 2014

Generate Sample XML from XSD using Visual Studio 2012

There was a question on how to generate sample XML from an XSD without using C# or VB.NET Code. I did some googling for a few hours and then stumbled upon a tutorial from Code project which is the reference of this post. Since, Im using Visual Studio 2012, I replicated the steps based from the article.
1. Add a new XSD file to Visual Studio. In my case, I reused the example from MSDN:
 <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>  
2. Open the file using XML Schema Explorer.
3. Right click on the XML Element which is PurchaseOrder and select Generate Sample XML:
4. Here's a sample generated XML File:
 <?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>  

Reference using Visual Studio 2010: Generate Sample XML from XSD

0 comments:

Post a Comment