Gokul's Blog

Programmatically create an XML document from BizTalk Schema

Leave a comment

programmatically create an XML document based on your XML schema? Or even better, what if you can use the schema that is validated and compiled by BizTalk and stored in the BizTalk management database?
This way you don’t have to worry about manually keeping the schema and the external XML file in synch since there is no external XML file any more. Any change you make in the schema is directly reflected in the Pipeline component when you build your new message. Well, it turns out that there is an undocumented API called “CreateXmlInstance” just for that. Here is the code snippet that does this.

using Microsoft.BizTalk.ExplorerOM;
using Microsoft.BizTalk.Component.Interop;
Add reference to the assemblies :
Microsoft.BizTalk.ExplorerOM ==> C:\Program Files\Microsoft BizTalk Server 2006\Developer Tools\Microsoft.BizTalk.ExplorerOM.dll,
Microsoft.BizTalk.Pipeline ==> C:\Program Files\Microsoft BizTalk Server 2006\Microsoft.BizTalk.Pipeline.dll

public XmlDocument CreateXmlDoc(string sSchemaName)
        {
            XmlDocument doc = new XmlDocument();
            BtsCatalogExplorer explorer = new BtsCatalogExplorer();

            explorer.ConnectionString = @"Integrated Security=SSPI; Server=localhost\SQL2005; Database=BizTalkMgmtDb;";
            Schema mySchema = explorer.Schemas[sSchemaName];

            if (mySchema != null)
            {
                DocumentSpec spec = new DocumentSpec(sSchemaName, mySchema.BtsAssembly.DisplayName);
                StringWriter sw = new StringWriter(new StringBuilder());
                doc.Load(spec.CreateXmlInstance(sw));
                sw.Dispose();
            }
            return doc;
        } 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }



This code goes directly to the BizTalk management database, gets the specified schema, and then create an empty XML document based on the schema.

From here on, you can programmatically populate/manipulate the XML element/attribute values as needed. One thing to note is that this code is not entirely immune to changes either. If you add new elements/attributes to your schema (which is the case for most situations), the code will be fine since it simply creates blank values for the new fields. However, if you remove some elements/attributes, or make changes to existing elements/attributes, and if your subsequent processing code access them, you will have to modify the code to accommodate the changes.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s