Msmq 元素不是类型的实例
我使用Linq到XSD通过MSMQ发送数据。这是我的发送代码Msmq 元素不是类型的实例,msmq,linq-to-xsd,Msmq,Linq To Xsd,我使用Linq到XSD通过MSMQ发送数据。这是我的发送代码 public void Send() { string criMessageQueuePath = ConfigurationManager.AppSettings["CRIMessageQueuePath"]; if (!MessageQueue.Exists(criMessageQueuePath)) { Me
public void Send()
{
string criMessageQueuePath = ConfigurationManager.AppSettings["CRIMessageQueuePath"];
if (!MessageQueue.Exists(criMessageQueuePath))
{
MessageQueue.Create(criMessageQueuePath, false);
}
var messageQueue = new MessageQueue(criMessageQueuePath) { Label = "CRI Message Queue" };
messageQueue.Formatter = new XmlMessageFormatter(new[] { typeof(XML) });
var transaction = new MessageQueueTransaction();
transaction.Begin();
messageQueue.Send(CreateAuction(1), transaction);
messageQueue.Send(CreateAuction(2), transaction);
messageQueue.Send(CreateAuction(3), transaction);
messageQueue.Send(CreateAuction(4), transaction);
messageQueue.Send(CreateAuction(5), transaction);
transaction.Commit();
}
private XML CreateAuction(int id)
{
var message = new XML {id = id};
return message;
}
这是我的接收代码
public IEnumerable<string> Receive()
{
string criMessageQueuePath = ConfigurationManager.AppSettings["CRIMessageQueuePath"];
var messageQueue = new MessageQueue(criMessageQueuePath);
Message[] messages = messageQueue.GetAllMessages();
foreach (Message message in messages)
{
message.Formatter = new XmlMessageFormatter(new[] { typeof(XML) });
yield return message.Body.ToString();
}
messageQueue.Purge();
}
它失败的行是var-typedXML=XML.Parse(XML)代码>。错误是
Element is not an instance of type Domain.XML
对象的XSD是
<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="Domain">
<xs:element name="XML">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我可以看到它产生了这个
"<XML>\r\n <XML xmlns=\"Domain\">\r\n <id>1</id>\r\n </XML>\r\n</XML>"
“\r\n\r\n 1\r\n\r\n”
换句话说,它围绕整个xml生成一个额外的xml节点。如果我在调试器中删除这个额外的节点,那么一切都会正常工作。我做错了什么?这是使用hte XmlMessageFormatter序列化对象并将其包装为XML的结果。我建议您将XML文档直接写入BodyStream属性
var msg = new Message();
var writer = new StreamWriter(msg.BodyStream);
writer.Write(xmlDoc.ToString());
writer.Flush();
queue.Send(msg);
这样,您就可以完全控制队列中发送的内容。格式化程序主要用于遗留用途,在将XML文档作为消息发送时不应使用。这是使用hte XmlMessageFormatter序列化对象并将其包装为XML的结果。我建议您将XML文档直接写入BodyStream属性
var msg = new Message();
var writer = new StreamWriter(msg.BodyStream);
writer.Write(xmlDoc.ToString());
writer.Flush();
queue.Send(msg);
这样,您就可以完全控制队列中发送的内容。格式化程序主要用于遗留用途,在将XML文档作为消息发送时不应使用
var msg = new Message();
var writer = new StreamWriter(msg.BodyStream);
writer.Write(xmlDoc.ToString());
writer.Flush();
queue.Send(msg);