Two fairly common operations in BizTalk Server 2004/6 are creating new messages from XML literal strings, as well as getting the XML text out of an existing BizTalk message in an orchestration.

The first option is very useful when creating new messages from scratch. However, the method I show here is not very useful for complex messages, because it would be very hard to maintain an read. For those scenarios, I really recommend Scott Colestock's option of creating messages from embedded resources in .NET assemblies; it's a much better option.

Here's some very small, yet useful piece of code to accomplish these tasks:


//
// XmlHelper.cs
//
// Author:
// Tomas Restrepo (tomasr@mvps.org)
//
using System;
using System.IO;
using System.Xml;
using Microsoft.XLANGs.BaseTypes;
using System;
using System.IO;
using System.Xml;
using Microsoft.XLANGs.BaseTypes;
namespace Winterdom.BizTalk.Util
{
///
/// Helper BizTalk class to deal with XML Based
/// messages
///

public class XmlHelper
{
///
/// Creates a typed BizTalk message
/// out of a string containing valid XML.
///

///
/// This does not validate the message at all.
///

/// XML content
/// The BizTalk Message
public static XmlDocument CreateMessage(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return doc;
}
///
/// Returns a string containing the XML from the message
///

/// Message
/// The xml
public static string GetMessageXml(XLANGPart part)
{
Stream s = (Stream)part.RetrieveAs(typeof(Stream));
using ( StreamReader reader = new StreamReader(s) ) {
return reader.ReadToEnd();
}
}
} // class XmlHelper
} // namespace Winterdom.BizTalk.Util


Tomas Restrepo

Software developer located in Colombia.