Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Programmatically Creating A New Message in BizTalk Pipeline Component

In the BizTalk Pipeline component, often times you need to construct new messages out of the received messages in the Disassemble stage before passing them on to the orchestration. This can be done in a few different ways.

The easiest way is to build a concatenated XML string using StringBuilder or something similar, and load it into a new XmlDocument object. However, this is very inflexible. If the structure of the message is changed, the Pipeline component would have to be recompiled and redeployed. Another approach will be to load the XML strings from a configuration file at runtime, or keep it as a separate resource file to be loaded at the runtime. This approach avoids the recompilation process when the XML structure is changed, but you still have to worry about keeping the external XML file in sync with the actual BizTalk schema file.

What if you can programmatically create an XML document based on the 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.

public XmlDocument CreateBTDoc(string schemaName)
XmlDocument doc = new XmlDocument();
BtsCatalogExplorer explorer = new BtsCatalogExplorer();

explorer.ConnectionString = @”Integrated Security=SSPI; Server=localhost\SQL2005; Database=BizTalkMgmtDb;”;
Schema schema = explorer.Schemas[schemaName];

if (schema != null)
DocumentSpec spec = new DocumentSpec(schemaName, schema.BtsAssembly.DisplayName);
StringWriter sw = new StringWriter(new StringBuilder());
return doc;

Essentially this code would go directly to the BizTalk management database, find the specified schema, and then create an empty XML document based on the schema. To use this code, you need to add references to Microsoft.BizTalk.ExplorerOM and Microsoft.BizTalk.Pipeline assemblies. Your using statements should include Microsoft.BizTalk.Component.Interop and Microsoft.BizTalk.ExplorerOM.

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.

1 Comment. Leave new

Hey, that was interesting,
thanks for the post

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>