无法以编程方式反序列化 SOAP 消息
当我尝试反序列化肥皂消息时,出现以下异常。我这样做是因为我有想要在测试中重用的响应文件。我无法使用真正的服务等,因为它不适合我们拥有的测试框架的架构。
Test 'MyUnitTestMethod' failed: System.InvalidOperationException : There is an error in XML document (1, 2).
----> System.InvalidOperationException : <MySpecialResponse xmlns='http://xsd.com/msgs/v1'> was not expected.
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
private const string _content =
@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
<soapenv:Body>
<ns3:MySpecialResponse xmlns:ns3=""http://xsd.com/msgs/v1"" >
<header>
<status>0</status>
</header>
<ns3:Payload>
<ns3:CustomerName>name</ns3:CustomerName>
<ns3:EmailAddress>[email protected]</ns3:EmailAddress>
</ns3:Payload>
</ns3:MySpecialResponse>
</soapenv:Body>
</soapenv:Envelope>";
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xsd.com/msgs/v1")]
public partial class MySpecialResponse : BaseResponse {
private MySpecialPayload payloadField;
/// <remarks/>
public MySpecialPayload Payload {
get {
return this.payloadField;
}
set {
this.payloadField;= value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xsd.com/msgs/v1")]
public partial class MySpecialPayload {
private string customerNameField;
private string emailAddressField;
/// <remarks/>
public string CustomerName {
get {
return this.customerNameField;
}
set {
this.customerNameField = value;
}
}
/// <remarks/>
public string EmailAddress {
get {
return this.emailAddressField;
}
set {
this.emailAddressField = value;
}
}
}
//The code I am using - might not be right?
var serialiser = new System.Xml.Serialization.XmlSerializer(typeof(MySpecialResponse));
using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(_content))
{
var doc = new System.Xml.XmlDocument();
doc.Load(stream);
var nsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
//so I can get the actual response and not the soap body
var node = doc.SelectSingleNode("//soapenv:Body", nsManager);
if (node != null)
{
byte[] xml = Encoding.UTF8.GetBytes(node.InnerXml);
using (var memStream = new System.IO.MemoryStream(xml))
{
memStream.Position = 0;
var resp = (MySpecialResponse)serialiser.Deserialize(memStream); //Throws exception here
}
}
}
(BaseResponse 具有标头字段)
我的异常发生的任何想法以及如何获取肥皂消息以将消息反序列化到对象中,无论是修复我的代码还是应用其他技术。
注意:我有很多文件想要使用通用方法进行反序列化,因此我不会提前知道所有 xml 命名空间,只知道根响应类型。
谢谢
I get the following exception when I try to deserialize a soap message. I am doing it this way cause I have the response files I want to reuse in testing. I cannot use a real service or the like as it does not fit the architecture for the testing framework we have.
Test 'MyUnitTestMethod' failed: System.InvalidOperationException : There is an error in XML document (1, 2).
----> System.InvalidOperationException : <MySpecialResponse xmlns='http://xsd.com/msgs/v1'> was not expected.
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
private const string _content =
@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
<soapenv:Body>
<ns3:MySpecialResponse xmlns:ns3=""http://xsd.com/msgs/v1"" >
<header>
<status>0</status>
</header>
<ns3:Payload>
<ns3:CustomerName>name</ns3:CustomerName>
<ns3:EmailAddress>[email protected]</ns3:EmailAddress>
</ns3:Payload>
</ns3:MySpecialResponse>
</soapenv:Body>
</soapenv:Envelope>";
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xsd.com/msgs/v1")]
public partial class MySpecialResponse : BaseResponse {
private MySpecialPayload payloadField;
/// <remarks/>
public MySpecialPayload Payload {
get {
return this.payloadField;
}
set {
this.payloadField;= value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xsd.com/msgs/v1")]
public partial class MySpecialPayload {
private string customerNameField;
private string emailAddressField;
/// <remarks/>
public string CustomerName {
get {
return this.customerNameField;
}
set {
this.customerNameField = value;
}
}
/// <remarks/>
public string EmailAddress {
get {
return this.emailAddressField;
}
set {
this.emailAddressField = value;
}
}
}
//The code I am using - might not be right?
var serialiser = new System.Xml.Serialization.XmlSerializer(typeof(MySpecialResponse));
using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(_content))
{
var doc = new System.Xml.XmlDocument();
doc.Load(stream);
var nsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
//so I can get the actual response and not the soap body
var node = doc.SelectSingleNode("//soapenv:Body", nsManager);
if (node != null)
{
byte[] xml = Encoding.UTF8.GetBytes(node.InnerXml);
using (var memStream = new System.IO.MemoryStream(xml))
{
memStream.Position = 0;
var resp = (MySpecialResponse)serialiser.Deserialize(memStream); //Throws exception here
}
}
}
(BaseResponse has the header field)
Any ideas my the exception is getting raised and how I can get the soap message to deserialize the message into the object whether it be fixing my code or applying another technology.
Note: I have many files that I want to ue a generic method for deserializing so I will not know ahead of time all the xml namespaces, only the root response type.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我已经自己解决了。
我需要为
XmlSerializer
添加XmlRootAttribute
Ok, I have solved it for myself.
I needed to add the
XmlRootAttribute
for theXmlSerializer