Byte Order Mark in xml messages generated from BizTalk does not render them invalid, but sometimes the recipient of the message fails in processing these marking. Out of the box, BizTalk provides the assembler component which can be configured to remove this byte order mark via the “Preserve Byte Order Mark” property. If however you already have a custom pipeline component on the send side, instead of adding an additional xml assembler I would rather update this pipeline component itself to do the job for me via a few extra lines of code.
So for e.g. am writing a customer encoder to remove namespaces from the outgoing message. A couple of statements whilst serializing the message allows me to get rid of the byte order mark without having to drag an drop an extra component. Following is an Execute snippet:
public IBaseMessage Execute(IPipelineContext inMsgContext, IBaseMessage inMsg)
{
Stream sourceMsgStream = inMsg.BodyPart.GetOriginalDataStream();
if (null != sourceMsgStream)
{
//Remove namespaces
XDocument msgDocument = XDocument.Load(sourceMsgStream);
msgDocument.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove();
foreach (XElement docElement in msgDocument.Descendants())
docElement.Name = docElement.Name.LocalName;
//Serialize to a stream and remove the byte order mark
Stream outMsgStream = new MemoryStream();
var settings = new XmlWriterSettings
{
Encoding = new UTF8Encoding(false),
Indent = true,
OmitXmlDeclaration = true
};
XmlWriter writer = XmlWriter.Create(outMsgStream, settings);
msgDocument.Save(writer);
writer.Flush();
if (outMsgStream.CanSeek)
outMsgStream.Position = 0;
inMsg.BodyPart.Data = outMsgStream;
return inMsg;
}
return null;
}
VeeN