【问题标题】:Cannot Deserialize a SOAP Message programmatically无法以编程方式反序列化 SOAP 消息
【发布时间】:2011-05-25 21:22:27
【问题描述】:

当我尝试反序列化一个肥皂消息时,我得到了以下异常。我这样做是因为我有想要在测试中重用的响应文件。我不能使用真正的服务等,因为它不适合我们拥有的测试框架的架构。

    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>test1@mail.com</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 命名空间,只知道根响应类型。

谢谢

【问题讨论】:

  • 请问您为什么需要像这样手动解析 SOAP 消息?
  • 我有一个在代码中手动创建响应的框架。我们有来自客户的响应文件,我想在某些情况下使用它们。
  • 如果有一种更简单/更好的方法可以让我获取肥皂响应文件并反序列化该文件,我将使用它。

标签: c# exception .net-3.5 soap xmlserializer


【解决方案1】:

好的,我自己解决了。

我需要为XmlSerializer 添加XmlRootAttribute

var xRoot = new System.Xml.Serialization.XmlRootAttribute();
xRoot.ElementName = "MySpecialResponse";
xRoot.IsNullable = true;
xRoot.Namespace = rootResponseNamespace;

var serialiser = new System.Xml.Serialization.XmlSerializer(typeof(MySpecialResponse), xRoot);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-09
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多