使用 XmlDictionaryReader 反序列化肥皂消息

Posted

技术标签:

【中文标题】使用 XmlDictionaryReader 反序列化肥皂消息【英文标题】:Deserializing soap message using XmlDictionaryReader 【发布时间】:2018-09-08 04:28:49 【问题描述】:

我正在尝试反序列化以下 SOAP 消息

<?xml version="1.0" encoding="UTF-8" standalone="no"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
  <ns3:findContractResponse xmlns:ns3="http://somewebsite.com/soapService">
    <return>123</return>
    <return>456</return>
  </ns3:findContractResponse>
</S:Body></S:Envelope>

这里是代码

using (Stream rs = response.GetResponseStream())

    using (var reader = XmlReader.Create(rs))
    
        Message m = Message.CreateMessage(reader, int.MaxValue, MessageVersion.Soap11WSAddressing10);
        XmlDictionaryReader dr = m.GetReaderAtBodyContents();
        DataContractSerializer dcs1 = new DataContractSerializer(typeof(findContractResponse), "findContractResponse", "http://somewebsite.com/soapService");
        findContractResponse fcr1 = (findContractResponse)dcs1.ReadObject(dr);
        long[] contracts1 = fcr1.@return;
    

fcr1 已正确设置为 findContractResponse 对象,但 fcr1.@return 为 NULL 而不是长数字列表。我在这里做错了什么?这是从 wsdl 生成的 findContractResponse:

[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] [System.ServiceModel.MessageContractAttribute(WrapperName="findContractResponse", WrapperNamespace="http://schemas.datacontract.org/2004/07/wsdltest.testHttpsClient", IsWrapped=true)] 公共部分类 findContractResponse [System.ServiceModel.MessageBodyMemberAttribute(Namespace= "http://schemas.datacontract.org/2004/07/wsdltest.testHttpsClient", Order=0)] [System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 公共长 [] @return; 公共 findContractResponse() 公共 findContractResponse(long[] @return) 这个。@return = @return;

【问题讨论】:

【参考方案1】:

经过几天的尝试,我找到了一种解决方法。我将发布我的解决方案,以防有人需要。

var response = (HttpWebResponse)httpRequest.GetResponse();
using (Stream rs = response.GetResponseStream())

    findContractResponse result = DeserializeResponse<findContractResponse>(rs);
    return result == null ? null : result.@return;


T DeserializeResponse<T>(Stream stream)

    using (StreamReader reader = new StreamReader(stream))
    
        Type type = typeof(T);
        XDocument doc = XDocument.Parse(reader.ReadToEnd());
        XElement data = doc.Descendants(XName.Get(type.Name, "http://somewebsite.com/soapService")).FirstOrDefault();
        if(data == null)
        
            return default(T);
        

        XmlSerializer ser = new XmlSerializer(type, "http://somewebsite.com/soapService");
        string tmpXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + data.ToString();
        using (StringReader stringReader = new StringReader(tmpXml))
        
            return (T)ser.Deserialize(stringReader);
        
    

【讨论】:

以上是关于使用 XmlDictionaryReader 反序列化肥皂消息的主要内容,如果未能解决你的问题,请参考以下文章

C语言怎么反序输出

pandas使用iloc函数将dataframe的所有数据行反序(reverse the order of rows in dataframe)

数字反序与数字的和

反序输出

Oracle运维案例之反序函数索引的使用

单链表反序