Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / XML
Alternative
Article

Generate Sample XML from XSD

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
8 Jun 2012CPOL 168K   12   14
This is an alternative for "Generate Sample XML from XSD".

Introduction

I spent considerable amount of time to know how to generate XML from a given XSD and I had great difficulties finding solution. That is why I am sharing this Tip which is I think useful.

Background 

We have XSD.EXE available to generate XML Schema (XSD) from a XML file but sometime we might need to work generate an XML for given XSD, specially in case of writing Service Client, where for Server has exposed Request XSD and Response XSD and want to start with client Request.

Generate Sample XML using Visual Studio 

Create an XSD File

Open/Create XSD file in Visual Studio

XML
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"  
    xmlns="http://tempuri.org/XMLSchema.xsd" 
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"  
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="AuthorInfor">
    <xs:annotation>
      <xs:documentation>this element will all authors for book</xs:documentation>
    </xs:annotation>
    <xs:restriction base='xs:string'>
      <xs:maxLength value='15'/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="IsdnInfo">
    <xs:annotation>
      <xs:documentation>this element defines 10 digit ISDN code</xs:documentation>
    </xs:annotation>
    <xs:restriction base='xs:string'>
      <xs:maxLength value='10'/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="BookShelfInfo">
    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="1">
        <xs:choice minOccurs="1" maxOccurs="5">
          <xs:element name="byAuthor" type="AuthorInfor"/>
        </xs:choice>
        <xs:element name="byISDNNo" type="IsdnInfo"/>
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="MyBookShelf" type="BookShelfInfo"/>
</xs:schema>

Open XML Schema Explorer

Open XML Schema Explorer by clicking on "XML Schema Explorer" or 'Use the XML Schema Explorer...'.

Image 1

You can also open "Schema Explorer" by clicking View menu.

Image 2

Generate XML File

If your Schema file is valid and you are having elements, then Right click on element and click on "Generate Sample XML", this functionality generates XML file in temp folder and open ups into Visual Studio.

Image 3

Generate Sample XML file generating XML like below:

XML
<?xml version="1.0" encoding="utf-8"?>
<MyBookShelf xmlns="http://tempuri.org/XMLSchema.xsd">
  <byAuthor>Chetan Bhagat</byAuthor>
  <byAuthor>Aditya Ghosh</byAuthor>
  <byAuthor>Reena Mehta</byAuthor>
</MyBookShelf>

Simply

Remember, this is quite easy to generate any sample XML file from XSD. Just think of it! 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Solution Architect with 19 years of experience in IT

Comments and Discussions

 
QuestionNot working Pin
7045Jeegnesh15-Jun-16 18:42
7045Jeegnesh15-Jun-16 18:42 
AnswerRe: Not working Pin
Joshi, Rushikesh16-Jun-16 4:20
professionalJoshi, Rushikesh16-Jun-16 4:20 
GeneralMy vote of 5 Pin
pmunari11-Dec-14 4:32
pmunari11-Dec-14 4:32 
GeneralRe: My vote of 5 Pin
Joshi, Rushikesh12-Jan-15 5:01
professionalJoshi, Rushikesh12-Jan-15 5:01 
QuestionGood post Pin
Vikas Tripathi 201224-Mar-14 19:35
Vikas Tripathi 201224-Mar-14 19:35 
GeneralMy vote of 5 Pin
sam viper28-Aug-13 11:38
sam viper28-Aug-13 11:38 
GeneralMy vote of 5 Pin
ChargerIIC10-Jul-13 5:58
ChargerIIC10-Jul-13 5:58 
GeneralRe: My vote of 5 Pin
Joshi, Rushikesh10-Jul-13 5:59
professionalJoshi, Rushikesh10-Jul-13 5:59 
QuestionFull XML Pin
rezaru200023-Apr-13 17:18
rezaru200023-Apr-13 17:18 
AnswerRe: Full XML Pin
Joshi, Rushikesh18-Jun-13 8:36
professionalJoshi, Rushikesh18-Jun-13 8:36 
AnswerThanks for sharing Pin
shaolinshaojie24-Sep-12 17:41
shaolinshaojie24-Sep-12 17:41 
GeneralRe: Thanks for sharing Pin
Joshi, Rushikesh25-Sep-12 9:03
professionalJoshi, Rushikesh25-Sep-12 9:03 
QuestionMy vote of 5. Pin
qian4040112-Jun-12 22:48
qian4040112-Jun-12 22:48 
AnswerRe: My vote of 5. Pin
Joshi, Rushikesh13-Jun-12 4:13
professionalJoshi, Rushikesh13-Jun-12 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.