XML 通过 MSMQ 发送的消息不包括编码
XML Message sent over MSMQ does not include encoding
我正在编写一个使用 MSMQ 的应用程序,我遇到了一个专门与 XML 声明标记的编码属性有关的问题。
我正在构建消息如下:
string xmlmsg = reqText.Text;
XmlDocument xdoc = new XmlDocument();
xdoc.Load(new StringReader(xmlmsg));
xdoc.InsertBefore(xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes"), xdoc.DocumentElement);
Message _msg = new Message();
_msg.BodyStream = new MemoryStream(Encoding.ASCII.GetBytes(xdoc.OuterXml));
reqQueue.Send(_msg, "XML Request");
xdoc.OuterXml 的控制台输出显示包含编码:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
但是当我通过 MSMQ 发送消息时,编码属性被删除。
<?xml version="1.0" standalone="yes"?>
我在这里错过了什么?
您错过了 XmlDeclaration 文档中的 note。
Note: If the XmlDocument is saved to either a TextWriter or an XmlTextWriter, this encoding value is discarded. Instead, the encoding of the TextWriter or the XmlTextWriter is used. This ensures that the XML written out can be read back using the correct encoding.
试试这段代码:
string xmlmsg = reqText.Text;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlmsg);
using (Message _msg = new Message())
using (var memStream = new MemoryStream())
using (var writer = XmlWriter.Create(memStream))
{
writer.WriteStartDocument(standalone: true);
xdoc.WriteTo(writer);
writer.Flush();
memStream.Seek(0, SeekOrigin.Begin);
_msg.BodyStream = memStream;
reqQueue.Send(_msg, "XML Request");
}
原来是编码错误。这是实际工作的简化代码:
Message _msg = new Message
{
Formatter = new XmlMessageFormatter(),
BodyStream = new MemoryStream(Encoding.Unicode.GetBytes(_xmlmsg))
};
reqQueue.Send(_msg, "XML Request");
它需要是 Unicode,而不是 ASCII。
我正在编写一个使用 MSMQ 的应用程序,我遇到了一个专门与 XML 声明标记的编码属性有关的问题。
我正在构建消息如下:
string xmlmsg = reqText.Text;
XmlDocument xdoc = new XmlDocument();
xdoc.Load(new StringReader(xmlmsg));
xdoc.InsertBefore(xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes"), xdoc.DocumentElement);
Message _msg = new Message();
_msg.BodyStream = new MemoryStream(Encoding.ASCII.GetBytes(xdoc.OuterXml));
reqQueue.Send(_msg, "XML Request");
xdoc.OuterXml 的控制台输出显示包含编码:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
但是当我通过 MSMQ 发送消息时,编码属性被删除。
<?xml version="1.0" standalone="yes"?>
我在这里错过了什么?
您错过了 XmlDeclaration 文档中的 note。
Note: If the XmlDocument is saved to either a TextWriter or an XmlTextWriter, this encoding value is discarded. Instead, the encoding of the TextWriter or the XmlTextWriter is used. This ensures that the XML written out can be read back using the correct encoding.
试试这段代码:
string xmlmsg = reqText.Text;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlmsg);
using (Message _msg = new Message())
using (var memStream = new MemoryStream())
using (var writer = XmlWriter.Create(memStream))
{
writer.WriteStartDocument(standalone: true);
xdoc.WriteTo(writer);
writer.Flush();
memStream.Seek(0, SeekOrigin.Begin);
_msg.BodyStream = memStream;
reqQueue.Send(_msg, "XML Request");
}
原来是编码错误。这是实际工作的简化代码:
Message _msg = new Message
{
Formatter = new XmlMessageFormatter(),
BodyStream = new MemoryStream(Encoding.Unicode.GetBytes(_xmlmsg))
};
reqQueue.Send(_msg, "XML Request");
它需要是 Unicode,而不是 ASCII。