a trace of thought on...BizTalk Server, Team Foundation Server, AppFabric, etc. RSS 2.0
 Wednesday, 11 May 2005

There have been several folks who have discussed how to create messages "from scratch" within an Orchestration context - you can read Matt's thoughts and check out a BizTalk documentation excerpt. This is a common question, and I had an additional technique I thought I would share... 

Background: When you have a schema that has many promoted properties (or distinguished fields), or many elements that can be set via xpath expressions easily, it can be useful to simply start with a "template" document instance and populate the element content that you are interested in. 

In this situation, you will often have a "Message Assignment" shape that looks something like this:

someMsg = xmlDoc;
someMsg.SomeElement = "some content";
// (or xpath(someMsg,someElementXPath) = "some content" if we don't have a 
// distinguished field.)

One disadvantage of loading up "template" xml documents from either expression shapes or code (via XmlDocument.LoadXml) is that those xml fragments can get easily "lost", and are hard to update early in the development cycle when schemas may still be in flux.  Loading the template files from the file system is problematic because the question arises "where should I store these files, so that I can find them in any environment I deploy to?" (Solvable, but a hassle.)

Instead, why not embed the template xml documents as assembly resources?  For those unfamiliar with that process, I have a short tutorial here (& a helper class.) 

  1. You will need a C# project as part of your overall BizTalk solution.  Place your template xml file(s) in the directory corresponding to this project, and add them as an "existing item" to the project. 
  2. Select this file within the Solution Explorer, and within the Properties window, select "Embedded Resource" as the "Build Action" as shown here:


  3. Place this class (text here) within the same C# project that houses the resources you have added.
  4. To construct a message, drag out a "Message Assignment" shape, and within the associated expression write some code like the following.  Simply pass the file name of the template document as an argument to GetXmlDocResource (or GetStringResource.)
    sampleUsingTemplate = 
       // Populate the "rest" of the message with distinguished fields, promoted 
       // properties, xpath expressions, etc.
       sampleUsingTemplate.SomeElement = "foo";

The class I have supplied will cache the loaded resources in a hashtable for performance sake, and allow you to load resources as both strings and XmlDocuments.

A last thought: Many people ask, "Why can't I just create a message using a new operator or a default constructor of some sort?" Well, because few XSD schemas sufficiently constrain the set of valid instance documents enough for that to be useful - what form would a "default message" take? (Would it have the optional elements you need? Some elements that you don't want?)

Enjoy - feedback appreciated!

Wednesday, 11 May 2005 15:29:14 (Central Standard Time, UTC-06:00)  #    Comments [6] -
BizTalk Tools | BizTalk Insights
Tracked by:
"BizTalk -" (Matt Meleski's .Net Blog - The ABC's of .NET) [Trackback]
"BizTalk -" (Matt Meleski's .Net Blog - The ABC's of .NET) [Trackback]
Thursday, 04 August 2005 10:42:28 (Central Standard Time, UTC-06:00)
Is there a particular reason the C# project must be included within the Biztalk solution? Could you build the EmbeddedResourceCache assembly separately and use it from multiple orchestrations?
Thursday, 04 August 2005 14:33:24 (Central Standard Time, UTC-06:00)
Jeremy - you can certainly uses the C# project from multiple orchestrations in multiple solutions. I have it bundled into the same solution just for convenience.
Monday, 28 November 2005 06:47:24 (Central Standard Time, UTC-06:00)
Hi Scott,

I've implemented this very common routine in another manner, allowing for better maintainable code in my opinion. Please see http://martijnh.blogspot.com/2005/10/schema-instance-generator-for-use.html. Let me know what you think, my e-mail address can be found here: http://martijnh.blogspot.com/2004/07/e-mail-address.html
Tuesday, 20 December 2005 08:32:08 (Central Standard Time, UTC-06:00)
I like the schema instance generator concept quite a bit - very nice. My only comment would be that for many schemas, you will have much of the structure optional (or minOccurs=0), and yet the instance document you need for a particular case might very well require that bit to be "filled in". The "generate instance" concept breaks down a bit there.
Thursday, 24 August 2006 03:50:50 (Central Standard Time, UTC-06:00)
Nice tip, Scott. Wish I had read this post sooner.
Wednesday, 02 May 2007 15:31:31 (Central Standard Time, UTC-06:00)
It seems too big elefant for such small mouse. :)
Comments are closed.
<2016 December>
About the author:

Scott Colestock lives, writes, and works as an independent consultant in the Twin Cities (Minneapolis, Minnesota) area.

© Copyright 2016
Scott Colestock
Sign In
All Content © 2016, Scott Colestock
DasBlog theme 'Business' created by Christoph De Baene (delarou)