Freigeben über


How to convert messages from ANSI to UTF8 encoding in a pipeline component

When an incoming message is sent in unrecognized ANSI encoding, we usually have to convert it into UTF8 or UTF16 for BizTalk to properly process it. This can be done within a simple custom pipeline component.

Below is the sample code:

  

publicIBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)

{

  //Validate parameters

if (pContext == null) thrownewArgumentNullException("pContext");

if (pInMsg == null) thrownewArgumentNullException("pInMsg");

this.pipelineContext = pContext;

this.baseMessage = pInMsg;

string partName;

for (int i = 0; i < baseMessage.PartCount; i++)

{

 

 MemoryStream outStream = newMemoryStream();

partName =

 

null;

IBaseMessagePart part = baseMessage.GetPartByIndex(i, out partName);

Encoding ANSI = Encoding.GetEncoding("big5");

Encoding UTF8 = Encoding.UTF8;

 

 StreamReader reader = newStreamReader(part.GetOriginalDataStream(), ANSI);

string partBody = reader.ReadToEnd();

byte[] ANSIBytes = ANSI.GetBytes(partBody);

byte[] UTF8Bytes = Encoding.Convert(ANSI, UTF8, ANSIBytes);

char[] UTF8Chars = newchar[UTF8.GetCharCount(UTF8Bytes, 0, UTF8Bytes.Length)];

UTF8.GetChars(UTF8Bytes, 0, UTF8Bytes.Length, UTF8Chars, 0);

partBody = new string(UTF8Chars);

StreamWriter writer = newStreamWriter(outStream, newUTF8Encoding());

 

writer.Write(partBody);

writer.Flush();

 

outStream.Seek(0,

 

SeekOrigin.Begin);

part.Data = outStream;

}

  

return baseMessage;

}

 

Best regards,

 WenJun Zhang