I found these two methods, looking for the answer to this question, and they really worked perfectly.
public System.Xml.XmlDocument ConvertToXMLDoc(System.Messaging.Message msg) { byte[] buffer = new byte[msg.BodyStream.Length]; msg.BodyStream.Read(buffer, 0, (int)msg.BodyStream.Length); int envelopeStart = FindEnvolopeStart(buffer); System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer, envelopeStart, buffer.Length - envelopeStart); System.ServiceModel.Channels.BinaryMessageEncodingBindingElement elm = new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement(); System.ServiceModel.Channels.Message msg1 = elm.CreateMessageEncoderFactory().Encoder.ReadMessage(stream, Int32.MaxValue); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(msg1.GetReaderAtBodyContents()); msg.BodyStream.Position = 0; return doc; } private int FindEnvolopeStart(byte[] stream) { int i = 0; byte prevByte = stream[i]; byte curByte = (byte)0; for (i = 0; i < stream.Length; i++) { curByte = stream[i]; if (curByte == (byte)0x02 && prevByte == (byte)0x56) break; prevByte = curByte; } return i - 1; }
Just call the ConvertToXmlDoc function, providing a message from the message queue, and you will return an XmlDocument. I am lazy, so I just drop innerXml into the file so that I can read it.
MessageQueue queue = new MessageQueue(queueName); var msg = queue.Receive(); var doc = ConvertToXMLDoc(msg); using (var sw = new StreamWriter(@"C:\message.txt"))) sw.Write(doc.InnerXml);
There is no app to buy, and you return your data to the code so that you can communicate with it.
PS: Credit, which should be a loan. Excerpt from http://social.msdn.microsoft.com/forums/en-US/wcf/thread/c03d80cd-492c-4ece-8890-6a35b12352e0 , which also refers to a more detailed discussion of the MSMQ encoding format.
bvoyelr
source share