BizTalk custom pipeline DECODE component to REPLACE a Special Character inside the Message

Friday, September 30, 2011

Hello Friends,

I am new to create a custom pipeline DECODE component but stuck at the logic which modifies the message (replacing some prespecified character with some default character).
What i know is what to REPLACE and with WHAT along with the format of file type which is Flat File.

For eg.
10ç0ç6140314101  02368A794 004 01000081224081223AAAPA    000000011270000000A000000024340000A         
3000000000000000A000000000000000A00000023858A00000023858A00000000000A00000000000A00000A 2AçAAVLAA    
4AAEAACAA AEACOA     AAAL EAUAAY AAVEAAOA                                                            
5                    000000000000000A000000000000000A000000000000000A                                
6             00000033917C00000033917CH06        AEAAE       000000000000000000000A0000000274311A    
7000ç000000A000000000000000000A0000000000        A00000000                                           
As you can see, there is one special character (ç : Cedilla), which i want to replace with "x"

Below is the code i was banging my head with

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
    if (inmsg.BodyPart != null)
    	IBaseMessagePart bodyPart = inmsg.BodyPart;
        Stream sOriginalStream = bodyPart.GetOriginalDataStream();
        if (sOriginalStream != null)
            MemoryStream memStream = new MemoryStream();
                Regex reReplaceChar = new Regex("ç", RegexOptions.None);
                StreamReader sReader = new StreamReader(sOriginalStream);
                StringBuilder sBuilder = null;
                byte[] byteArray = new byte[sOriginalStream.Length];
                while (sReader.Peek() != -1)
                    sBuilder = new StringBuilder(sReader.ReadLine());
                    sBuilder = new StringBuilder(reReplaceChar.Replace(sReader.ReadLine().ToString(), "x"));
                    byteArray = System.Text.Encoding.ASCII.GetBytes(sBuilder.ToString());
                    memStream.Write(byteArray, 0, byteArray.Length);
                bodyPart.Data = memStream;
                memStream.Position = 0;
                inmsg.BodyPart.Data = bodyPart.Data;
            catch (Exception ex)
                System.Diagnostics.EventLog.WriteEntry("ReplaceCharacter", "Exception Occurred : " + ex.Message, EventLogEntryType.Error, iEventID, 5);
            	sBuilder = null;
    return inmsg;
Problem area for me is:
1. How to loop throu the message?
2. Replace doesn't work, so i created another function which find out the position of the character, remove it and insert "x" (the Replacement Character) at the same position.
3. Not getting the proper message in output.

I am stuck very badly, please help me. 
Thanks a tonne in advance.

Your code is very close - I haven't tried it, but from reading it I think the only problem may be that you're closing the memory stream? 
you're assigning your memory stream to the message you're returning to BizTalk, but then your'e closing the stream, which would inevidebly mean BizTalk won't be able to read the message?

You've correctly added it to the resource tracker which would allow Biztalk to dispose of it when done processing the message, so you should be ok removing the two lines from the finally block.

I would emphasie again the points I've made yesterday - your code will read the entire message to memory, which may cause issues under load, and you really ought to be createing a new message and not replace the stream on the existing one (but while I'm quite strickt on the first point I'll admit to be more 'flexible' on the second sometimes)
 Here, do this - it should work:

The line:

      sBuilder = new StringBuilder(sReader.ReadLine());

Replace with:

      sBuilder = new StringBuilder(sReader.ReadLine(), Encoding.Default);

This will allow your code to recognize your cedilla character.

Then just have ONE string replace line like this in your loop:

      sBuilder = new StringBuilder(sReader.ReadLine().Replace("ç", "x")); 

You don't need any of that Regex stuff.
Here's the test method I just wrote to validate the above.  You can use it for reference.

        private static string BizTalkHelp(string input)
                StreamReader sReader = new StreamReader(@"C:\BizTalkTestDir\sample.txt", Encoding.Default);
                StringBuilder sBuilder = null;
                while (sReader.Peek() != -1)
                    sBuilder = new StringBuilder(sReader.ReadLine().Replace("ç", "x"));
                return sBuilder.ToString();

No comments:

Post a Comment

Post Your Comment...