Streamed XPath Extraction using hidden BizTalk class XPathReader
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
Anonymous
May 27, 2013
The comment has been removedAnonymous
November 17, 2013
Yes you are right and in this post I was not trying to perform full streamed pipeline I was trying to avoid simply loading the entire XML in memory as this consumes resources and time.Anonymous
February 26, 2014
It is not streaming approach, you could enhance it by encapsulating the original stream in Virtual Stream firstly, still it is a good practice to read the message without highly consuming the memory, for full streaming you need to write your custom stream implementing Read method, or for Xml messages simply use XPathMutatorStream or XmlTranslatorStream