反序列化SOAP响应

时间:2018-06-29 19:16:00

标签: c# soap

数据是从Apache Web服务返回的,所以我无法使用int,因为生成的类无法进行Web服务调用或返回数据

我正在尝试在C#.NET中反序列化以下SOAP响应。我可以验证Add Service Reference语句是否正确提取我要反序列化的实际响应(取自this answer),但是当我检查响应对象时,XDocument和{{1 }}列表为Attribute1

任何帮助将不胜感激。

SOAP响应

MapItem

C#类

null

反序列化代码

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <ns1:checkcompanyResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
   <checkcompanyReturn xsi:type="ns2:Map" xmlns:ns2="http://xml.apache.org/xml-soap">
    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
     <key xsi:type="soapenc:string">Entry1</key>
     <value xsi:type="soapenc:string">Y</value>
    </item>
    <item>
     <key xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Entry2</key>
     <value xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">N</value>
    </item>
   </checkcompanyReturn>
  </ns1:checkcompanyResponse>
 </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:1)

请使用此模型

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace yournamespace
{
    [XmlRoot(ElementName="key")]
    public class Key {
        [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
        [XmlText]
        public string Text { get; set; }
        [XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenc { get; set; }
    }

    [XmlRoot(ElementName="value")]
    public class Value {
        [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
        [XmlText]
        public string Text { get; set; }
        [XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenc { get; set; }
    }

    [XmlRoot(ElementName="item")]
    public class Item {
        [XmlElement(ElementName="key")]
        public Key Key { get; set; }
        [XmlElement(ElementName="value")]
        public Value Value { get; set; }
        [XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenc { get; set; }
    }

    [XmlRoot(ElementName="checkcompanyReturn")]
    public class CheckcompanyReturn {
        [XmlElement(ElementName="item")]
        public List<Item> Item { get; set; }
        [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
        [XmlAttribute(AttributeName="ns2", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Ns2 { get; set; }
    }

    [XmlRoot(ElementName="checkcompanyResponse", Namespace="http://DefaultNamespace")]
    public class CheckcompanyResponse {
        [XmlElement(ElementName="checkcompanyReturn")]
        public CheckcompanyReturn CheckcompanyReturn { get; set; }
        [XmlAttribute(AttributeName="encodingStyle", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
        public string EncodingStyle { get; set; }
        [XmlAttribute(AttributeName="ns1", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Ns1 { get; set; }
    }

    [XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body {
        [XmlElement(ElementName="checkcompanyResponse", Namespace="http://DefaultNamespace")]
        public CheckcompanyResponse CheckcompanyResponse { get; set; }
    }

    [XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope {
        [XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenv { get; set; }
        [XmlAttribute(AttributeName="xsd", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xsd { get; set; }
        [XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
    }

}