Determining the Type of an XML Message in a Custom Pipeline Component

Most of the time, when using builtin pipelines in BizTalk, you don’t have to worry about determining the type of a incoming message. This is actually all taken care of by the Xml Disassembler or the Flat File Disassembler pipeline components.

In more complex scenarios, however, you sometimes need to determine the type of an incoming message yourselves. This can happen, for instance, if you do not want or simply cannnot use the builtin pipeline components. Say, you might have already a custom pipeline component in the disassemble stage and you might need to determine the type of one or more incoming messages in the decode stage.

Introducing the MarkableForwardOnlyEventingReadStream Class

As you know, determining the type of an XML message involves reading the first few characters until you can accurately detect the root tag as well as its namespace. So, it’s very easy to write a quick function to do this.

However, since we’re in a pipeline component, we need to make sure the contents of the message goes through the pipeline in a consistent way. If we read a few characters from the incoming stream, we need to make sure these characters make it through to the end. So we might need to buffer these and hand them over when requested by downstream components.

In fact, this is exactly what the MarkableForwardOnlyEventingReadStream is for. This builtin class is buried inside the Microsoft.BizTalk.Streaming assembly which is only available in the GAC. This class gives a a means, among other things, to mark the current position of the stream, read from it, and virtually reset the position of the stream to the marked position.

Determining the Message Type

As we’ve seen in a previous article, it’s a simple matter to wrap the original stream of the incoming message to this custom class so that subsequent pipeline components can get at the data that’s already been read as part of our effort to check the root tag and xml namespace of the XML message.

With all these considerations out of the way, it’s time to write the code. That’s when you notice that the Microsoft.BizTalk.Streaming assembly, mentioned above, contains a little gem that is actually doing most of the work for us!

The following code, therefore, shows what is probably the simplest function to determing the type of an incoming XML message in a custom pipeline component:

private string RecognizeMessageType

(IPipelineContext pContext, IBaseMessage pInMsg)
    MarkableForwardOnlyEventingReadStream stream =
        new MarkableForwardOnlyEventingReadStream(

        string messagetype = Utils.GetDocType(stream);

        WriteEventLog(Resources.RecognizingMessageType, messagetype);

        pInMsg.BodyPart.Data = stream;

        return messagetype; 

    catch (System.Exception /* e */)

Notice the last few lines of this function. This substitutes the original data stream of the incoming message with the custom stream and registers this new stream with the BizTalk Resource Tracker. As we’ve seen already, these are critical to ensure that the custom stream is not garbage collected until it has a chance to be used by downstream components.

Next time, we’ll see how to make this simple function to use.

This entry was posted in Tips. Bookmark the permalink.

One Response to Determining the Type of an XML Message in a Custom Pipeline Component

  1. Pingback: A Custom FlatFile Schema Resolver and Disassembler Pipeline Component | A Technical Perspective

Comments are closed.