BizTalk XPATHReader

Code snippet for using the XPATHReader in Microsoft.BizTalk.XPATHReader.dll.  You’ll find this assembly in the GAC on any BizTalk Server installation.  XML below is compatible with the XPATH’s in the PromoteContext method.

<Msg>
  <Id>732</Id>
  <CreateDate>2016-05-30T09:55:37.257</CreateDate>
<Msg>

Add the below code to a pipeline component then a reference to Microsoft.BizTalk.Streaming.dll and Microsoft.BizTalk.XPATHReader.dll.

using Microsoft.BizTalk.Streaming;
using Microsoft.BizTalk.XPATH;

private void PromoteContext(IBaseMessage pInMsg)
{
  ReadOnlySeekableStream vs = new ReadOnlySeekableStream(pInMsg.BodyPart.Data);
  XmlReader reader = XmlReader.Create(vs);

  XPathCollection xPathCollection = new XPathCollection();
  int idQuery = xPathCollection.Add("/*[local-name()='Msg']/*[local-name()='Id']");
  int createDateQuery = xPathCollection.Add("/*[local-name()='Msg']/*[local-name()='CreateDate']");

  XPathReader xPathReader = new XPathReader(reader, xPathCollection);

  while (xPathReader.ReadUntilMatch())
  {
    if (xPathReader.Match(idQuery))
    {
      pInMsg.Context.Promote("Id", "https://Schema.PropertySchema", xPathReader.ReadString());
    }

    if (xPathReader.Match(createDateQuery))
    {
      pInMsg.Context.Promote("CreateDate", "https://Schema.PropertySchema", xPathReader.ReadString());
    }
  }

  // Set the ForwardOnlyStream to a seekable stream
  pInMsg.BodyPart.Data = vs;
  pInMsg.BodyPart.Data.Seek(0, SeekOrigin.Begin);
}
Advertisements
This entry was posted in BizTalk Server. Bookmark the permalink.

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