BizTalk– Extracting xs:Base64Binary

Hi,

Sometimes you will need to extract binary data from an XML file. It may be in a form of a string or Base64Binary, either way, you can extract it by using a helper class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.XLANGs.BaseTypes;
using System.IO;


namespace Romiko.Common.Helpers
{
    [Serializable]
    public  class CustomStreamFactory : IStreamFactory
    {

        byte[] _data;

        public CustomStreamFactory(byte[] b)
        {
            _data = b;
        }

        public Stream CreateStream()
        {
            return new MemoryStream(_data);
        }

    }
}

 

Then all we need is to create a helper method:

using System;
using Microsoft.XLANGs.BaseTypes;

namespace Romiko.Common.Helpers
{
    [Serializable]
    public  static class Utilities
    {

        public static void GetBinary(XLANGMessage message, string base64data)
        {
            byte[] b = System.Convert.FromBase64String(base64data);
            message[0].LoadFrom(new CustomStreamFactory(b));

        }

        public static void GetBinary(XLANGMessage message, byte[] base64data)
        {
            message[0].LoadFrom(new CustomStreamFactory(base64data));

        }
    }
}

 

I created two overloads here, in case you have it in a byte array in XML or as a string.

In an orchestration you can then create a new message in a message construct like so:

image

System.Diagnostics.Trace.WriteLine(">>> PersistCompletedApplication.odx - Sending Pdf");

GeneratedPdf = new System.Xml.XmlDocument();

//Helper to Extract Binary data into a new binary message.
Romiko.Common.Helpers.Utilities.GetBinary(GeneratedPdf, MyXmlDocument.GeneratedPdf);
GeneratedPdf(FILE.ReceivedFileName) = nFileName + ".pdf";

MyXmlDocument.GeneratedPdf is a distinguished field here, so it is easier to pass the data over, not the most memory efficient, but in my case the documents (PDF) are tiny, so no need to over engineer the solution.

Thanks to Richard Seroter’s tips Smile

Cheers

Advertisements
Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s