本文介绍了无法反序列化SOAP消息编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,当我尝试反序列化SOAP消息得到下面的异常。我做这种方式,因为我有我想重用在测试中的响应文件。我不能用一个真正的服务或类似的,因为它不符合架构的测试框架,我们有。

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有报头字段)

(BaseResponse has the header field)

我的任何异常被越来越引发的想法,我怎么能得到SOAP消息反序列化消息到对象是否可以修复我的code或应用另一种技术。

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.

注:我有,我想UE泛型方法反序列化,所以我不会提前知道当时许多文件中的所有XML命名空间,只有根响应类型

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.

感谢

推荐答案

好吧,我已经解决了它自己。

Ok, I have solved it for myself.

我需要添加 XmlRootAttribute 的XmlSerializer

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);

这篇关于无法反序列化SOAP消息编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:09