Insert XML Elements with the BizTalk Rules Engine

I have a case where I need to validate an incoming XML message and generate an aggregated XML message containing all validation failures. The base functionality of the BRE does not allow this kind of message manipulation – adding new elements (nodes) to messages and then setting the value.

My good friend Bing to the rescue!

This blog post gives a good walkthrough of doing exactly what I needed to do using the Microsoft.RuleEngine.XMLHelper class. Pay close attention to the Windows Registry setting at the end of the post.

This MSDN blog had a very good sample BRE Policy that I used to see a concrete example. Download the .exe and extract it.

This MSDN document details the Windows Registry setting that I mentioned previously. The function is to allow the BRE to use .NET helper methods as static objects, rather than requiring an instance.

These three sources combined gave me the complete solution. It works like a charm, and I did not have to write my own custom .NET helper class to do the XML element insertion.

Good times!


Removing Non-ASCII Characters With RegEx

I got some large XML files that had invalid characters in them and would not process. I ran a script on them to replace non-ASCII characters with empty strings. Here is the snippet.

string source = File.ReadAllText(@"C:\SourceFile.xml");

string dest = Regex.Replace(source, @"s/[^\x20-\x7E|\n|\ |\t]//g", string.Empty);

File.WriteAllText(@"C:\DestFile.xml", dest);

For the record, I got the RegEx here