Team blog of MCS @ Middle East and Africa

This blog is created by Microsoft MEA HQ near shoring team, and it aims to share knowledge with the IT community.With its infrastructure and development sides,It brings to you the proven best practices and real world experiences from Subject Matter Experts
Follow Us On Twitter! Subscribe To Our Blog! Contact Us

Streamed XPath Extraction using hidden BizTalk class XPathReader

Streamed XPath Extraction using hidden BizTalk class XPathReader

  • Comments 1
  • Likes

Usually when writing custom BizTalk pipeline components you find yourself wanting to extract specific values from the message passed using Xpath statements.

You can do this either by XPathDocument or XDocument, but this solution would require loading the entire XML into memory and if the XML file is huge that can be not possible. Also it makes the pipeline component slower. The solution is to use a streamed class such as XMLReader. But that would be too much work to do, right?

The solution comes in the form of a hidden GEM in the BizTalk installed components, called the XPathReader. This is a stream based class that would search for a node or element using the given set of XPath strings.

This class is defined in the assembly Microsoft.BizTalk.XPathReader.dll deployed to the GAC. You need to add a reference to this assembly first and then use the class as below.

            MsgStream.Seek(0, SeekOrigin.Begin);

            XmlReader reader = XmlReader.Create(MsgStream, settings);

            string strValue = null;

            if (!string.IsNullOrEmpty(MsgXPath))

            {

                XPathCollection xPathCollection = new XPathCollection();

                XPathReader xPathReader = new XPathReader(reader, xPathCollection);

                xPathCollection.Add(MsgXPath);

                if (xPathReader.ReadUntilMatch())

                {

                    if (xPathReader.Match(0))

                    {

                        strValue = xPathReader.ReadString();

                    }

                }

                MsgStream.Seek(0, SeekOrigin.Begin);

            }

Where the MsgStream is a seekable steam obtained from the message.

Comments
Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment