Pages

BizTalk Q & A: Convert pipeline message to XDocument

Thursday, December 27, 2012
Q:
How can I transform an  Microsoft.BizTalk.Message.Interop.IBaseMessaget to XDocument to make changes in the message. And how I canrecreate my message after changes ?publicMicrosoft.BizTalk.Message.Interop.IBaseMessageExecute(Microsoft.BizTalk.Component.Interop.IPipelineContextpc,                                                                      Microsoft.BizTalk.Message.Interop.
IBaseMessageinmsg)        {
returninmsg;        }
thank you
Sol:
public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            //To get Incoming message
            System.IO.Stream originalStream = pInMsg.BodyPart.GetOriginalDataStream();
            //Working with XDocument
            XDocument xDoc;
            using (XmlReader reader = XmlReader.Create(originalStream))
            {
                reader.MoveToContent();
                xDoc = XDocument.Load(reader);
            }
            
            // Returning stream
            byte[] output = System.Text.Encoding.ASCII.GetBytes(xDoc.ToString());
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.Write(output, 0, output.Length);
            memoryStream.Position = 0;
            pInMsg.BodyPart.Data = memoryStream;
            return pInMsg;
        }

or
Converting it to XmlDocument:
// Load original message to XmlDocument
Stream originalMessage = inMsg.BodyPart.GetOriginalDataStream();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(originalMessage);
//
//Set output message
IBaseMessage outMsg = pipelineContext.GetMessageFactory().CreateMessage();
outMsg.AddPart("Body", pipelineContext.GetMessageFactory().CreateMessagePart(), true);
Stream outputStream = new MemoryStream();
xmlDoc.Save(outputStream);
outputStream.Position = 0;
outMsg.BodyPart.Data = outputStream;
return outMsg;

For additional information, check the following links

No comments:

Post a Comment

Post Your Comment...