Generating XML instance from XSD Schema
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”
Comments RSS
TrackBack Identifier URI
Leave a comment

This is really usefull code, I was searching for this 5 hours. Atlast i got it from this page.
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.
Yes! Thanks for pointing that out :)