Back in November I wrote a post about encoding messages as base64 strings in a BizTalk map. I never added the explicit implementation of how to decode the message though. However, I was asked to provide an example of it, so here is part 2: how to decode a base64 string from an XML message and output it in BizTalk.
Consider the scenario to be the opposite of what we did in part 1.
We have a message on the input side that has two fields, one with an id as a string, and another string element that holds the entire base64 encoded string.
On the output side, we want to have a message that conforms to the schema we have defined. The message is the entire contents of the base64 encoded string in the input.
If we parse the encoded string, we get:
Which conforms fully to the schema defined.
So, the task is to extract the contents from the EncodedData element, decode them, and use the full string as the output message. All in a single BizTalk map.
The map will look like this to begin with, with the two schemas chosen:
Similar to when we encoded, we add first a scripting functoid that has no connections, and paste the code for the decode function in it:
public string Base64DecodeData(string param1)
byte decodedBytes = Convert.FromBase64String(param1);
We simply take a string as an input parameter, decode this and return the decoded string back.
Then we create a new scripting functoid that we set to inline Xsl and paste this code into it:
<xsl:value-of select="//EncodedData" />
<xsl:value-of select="userCSharp:Base64DecodeData($data)" disable-output-escaping="yes" />
This functoid is connected to the output root node giving us a map that looks like this:
When executing this map with the input message above, we will get the output message properly formatted.
The tricks used here is two. First off, we use the same pattern as before with calling a predefined function we have in another scripting functoid in order to call a simple C# function from our XSL code. Then in the XSL, we first extract the string from our input message and store it in a variable. This is then passed into our C# function and we get a string back. However, if we do not specify the disable-output-escaping="yes" in our value-of select, we would get the string entity encoded. With this extra property set, the string will be output as it is and the way we want it.
The same technique can of course easily be used to just output part of a message by simply connecting the scripting functoid to the node you want to populate (if for instance you have a schema that has a node defined as xs:any that you want to use).