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.