I am attempting to read the raw XML of an MSMQ message and save it to an XML file. For instance, the MSMQ message with body:
<?xml version="1.0"?>
<string>Hello World! I am message #4</string>
I would like to take those exact xml lines and save them to a file message.xml.
I have the message reading like the below:
msg = queue.PeekById(enumerator.Current.Id);
msg.Formatter = new XmlMessageFormatter(new String[] { "System.String,mscorlib" });
System.IO.File.WriteAllText(dirPath + @"\" + msg.Label.Replace(@"\","").Replace(@".","") + enumerator.Current.Id.Replace(@"\","").Replace(@".","") + "_" + DateTime.Now.ToString("MMddyyyyhhmmss") + ".xml", msg.Body.ToString());
queue.ReceiveById(enumerator.Current.Id);
logEntry("*-Received Message with Id " + msg.Id + " and Label " + msg.Label);
But that only pulls the text "Hello World! I am message #4" and I want the full xml.
Try this:
msg.Formatter = new ActiveXMessageFormatter();
reader = new StreamReader(msg.BodyStream);
var msgBody = reader.ReadToEnd(); // This gets the actual message as text
Formatters do not need to be symmetric - you can have different formatters on either end of the queue.
From here - http://andypiper.co.uk/2006/03/31/receiving-plain-text-messages-in-msmq/