简体   繁体   English

以原始XML读取MSMQ消息

[英]Reading MSMQ Messages in Raw XML

I am attempting to read the raw XML of an MSMQ message and save it to an XML file. 我试图读取MSMQ消息的原始XML并将其保存到XML文件。 For instance, the MSMQ message with body: 例如,带有正文的MSMQ消息:

<?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. 我想采用那些确切的xml行并将它们保存到文件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. 但这只是拉文本“Hello World!我是消息#4”,我想要完整的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/ 从这里 - http://andypiper.co.uk/2006/03/31/receiving-plain-text-messages-in-msmq/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM