简体   繁体   English

向 MSMQ 发送 XML 消息,不进行任何格式化

[英]Send XML message to MSMQ without any formatting

I need to send an XMLDocument object to MSMQ.我需要将 XMLDocument object 发送到 MSMQ。 I don't have a class to deserialize it into (the xml may vary).我没有 class 将其反序列化为(xml 可能会有所不同)。 The default formatter, XMLMessageFormatter, will “pretty print” the object.默认格式化程序 XMLMessageFormatter 将“漂亮地打印” object。 This causes a problem since这会导致一个问题,因为

<text></text>

Will be converted to将转换为

<text>

</text>

(Ie cr + spaces). (即 cr + 空格)。 The message is being read by a process using the default XMLMessageFormatter and hasn't been an issue whilst nodes have data in them.该消息正在由使用默认 XMLMessageFormatter 的进程读取,并且在节点中有数据时没有问题。 This is an issue, however, further down the line, as a process (out of my control) will interpret these new characters as data and cause an error.然而,这是一个问题,因为进程(我无法控制)会将这些新字符解释为数据并导致错误。

I know I could write some code to convert them using IsEmpty = true giving <text /> but I'd like a solution that doesn't alter the object at all.我知道我可以编写一些代码来使用 IsEmpty = true 给<text />来转换它们,但我想要一个完全不改变 object 的解决方案。

BinaryMessageFormatter will prefix the data with BOM data (receiver is not expecting that) and ActiveXMessageFormatter will double byte the string (again causing issues the other end). BinaryMessageFormatter 将在数据前面加上 BOM 数据(接收方不希望这样),而 ActiveXMessageFormatter 会将字符串加倍字节(再次导致另一端出现问题)。

I would rather like to avoid having to write a custom message formatter.我宁愿避免编写自定义消息格式化程序。 I've tried including some options in the XMLMessageFormatter but they've had little effect.我尝试在 XMLMessageFormatter 中包含一些选项,但效果不大。 Any ideas would be very much appreciated.任何想法将不胜感激。

MSMQ operates on raw blobs. MSMQ 对原始 blob 进行操作。 You do not have to use a formatter unless you want to.除非您愿意,否则您不必使用格式化程序。

To send a message and get it back byte-for-byte identical, use the BodyStream property.要发送一条消息并逐个字节地取回相同的消息,请使用BodyStream属性。

Example:例子:

var queue = new MessageQueue(@".\private$\queueName");
var msg = new Message();
msg.BodyStream = new MemoryStream(Encoding.UTF8.GetBytes("<root><test></test></root>"));
queue.Send(msg);

Resultant message:结果消息:

显示不受干扰的 XML 的屏幕截图

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

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