Generating XML instance from XSD Schema

Written by Manas Jan 18

Its easy to programatically generate XML instances from XSD schema if you have BizTalk libraries installed. All you need to do is this (Replace “http://schemas.microsoft.com/BizTalk/2003″ with the Target Namespace of your XSD):

XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
schemaCollection.Add(null, “c:\\schema1.xsd”);
System.Xml.XmlQualifiedName rootName = new System.Xml.XmlQualifiedName(“BAPI_PO_GETDETAIL_Request”);
DocumentSchema docSchema = new DocumentSchema(schemaCollection, “http://schemas.microsoft.com/BizTalk/2003″, rootName );
Stream stream = docSchema.CreateXmlInstanceWithData();
//Do whatever you feel like with the stream!

The class Microsoft.BizTalk.ParsingEngine.DocumentSchema can be accessed by adding a reference to Microsoft.BizTalk.Pipeline.dll.

Update : To do the same without using BizTalk assemblies, this code will help you : http://msdn2.microsoft.com/en-us/library/aa302296.aspx

3 Responses to “Generating XML instance from XSD Schema”

  1. Comment by Manikandan.net INDIA Windows Server 2003 Internet Explorer 6.0 on February 20, 2008 8:53 am

    This is really usefull code, I was searching for this 5 hours. Atlast i got it from this page.

  2. Comment by Manikandan.net INDIA Windows Server 2003 Internet Explorer 6.0 on February 20, 2008 8:55 am

    DocumentSchema docSchema = new DocumentSchema(schemaCollection, “http://schemas.microsoft.com/BizTalk/2003″, rootName );

    Above line given error !!!!! (object ref not found)

    To Fix: I replaced “http://schemas.microsoft.com/BizTalk/2003″ to actual schemas TargetNamespace.

  3. Comment by manas UNITED STATES Windows XP Internet Explorer 7.0 on February 20, 2008 11:03 am

    Yes! Thanks for pointing that out :)

Comments RSS rss Manas Garg Speaks TrackBack Identifier URI rss Manas Garg Speaks

Leave a comment