Martijn's blog - E-Commerce, EAI, BizTalk and .NET

2006/03/01

XPathMutatorStream

It seems this little gem is hidden as almost no one I talk to knows of it's existence.

Within the Microsoft.BizTalk.XPath namespace in the (GAC) assembly Microsoft.BizTalk.XPathReader.dll, a class is contained called XPathReader. Relying on this reader it's possible to match XPath expressions in a streaming fashion. This information is out on the web and discussed on many occasions.

However, within the Microsoft.BizTalk.Streaming namespace in the (GAC) assembly Microsoft.BizTalk.Streaming.dll, another class named XPathMutatorStream is defined. The class does what it's name implies, it allows for a callback to alter the value of specified XPathExpressions (hence the reference to XPathReader first). A basic implementation could look like this:

---

MemoryStream stream = new MemoryStream();

StreamWriter sw = new StreamWriter(stream);

string xmldocInstance = "bla";

sw.WriteLine(xmldocInstance);
sw.Flush();
stream.Position = 0;

XPathCollection queries = new XPathCollection();

queries.Add(new XPathExpression("/*[local-name()='Order']/*[local-name()='OrderLines']/*[local-name()='OrderLine']"));

ValueMutator mutator = new ValueMutator(this.XPathCallBack);

XPathMutatorStream mutatorStream = new XPathMutatorStream(stream, queries, mutator);

XmlTextReader reader = new XmlTextReader(mutatorStream);

while (reader.Read());

***

private void XPathCallBack(
 int matchIdx,
 XPathExpression matchExpr,
 string origVal,
 ref string finalVal)
{
 MessageBox.Show("hit!");

 finalVal = "bli";
}

200 Comments:

Post a Comment

<< Home