尝试在C#中使用DataContractSerializer反序列化SOAP信封

时间:2015-02-28 17:48:29

标签: c# xml soap deserialization datacontractserializer

我使用XmlSerializer工作了,但想使用DataContractSerializer来比较性能。我知道我很接近,因为我至少得到了一些对象图,但没有任何数据。我试图反序列化的XML示例如下所示:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Header/>
    <soap-env:Body>
       <n0:OperationalDataProvisioningFetchDataDirectResponse xmlns:n0="http://sap.com/bw" xmlns:prx="proxy">
           <BinaryObject>Binary Data Goes Here</BinaryObject>
           <XMLFormatUsedCode>ABAP_XML</XMLFormatUsedCode>
       </n0:OperationalDataProvisioningFetchDataDirectResponse>
    </soap-env:Body>
</soap-env:Envelope>

我要反序列化的类看起来像这样:

[DataContract(Name = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
    [DataMember(Name = "Header", Order = 0)]
    public object Header;

    [DataMember(Name = "Body", Order = 1)]
    public EnvelopeBody Body;
}

[DataContract(Name = "Body")]
public  class EnvelopeBody
{
    [DataMember(Name = "OperationalDataProvisioningFetchDataDirectResponse", Order = 0)]
    public FetchDataDirectResponse FetchDataDirectResponse;
}

[DataContract(Name = "OperationalDataProvisioningFetchDataDirectResponse", Namespace = "http://sap.com/bw")]
public  class FetchDataDirectResponse
{
    [DataMember(Name = "BinaryObject", Order = 0)]
    public string BinaryObject;

    [DataMember(Name = "XMLFormatUsedCode", Order = 1)]
    public string XMLFormatUsedCode;
}

当我运行它并尝试反序列化时,这就是我得到的:

Debugger

1 个答案:

答案 0 :(得分:2)

我明白了。我没有为DataContract for Body使用正确的命名空间(如果有的话)。这修好了它:

[DataContract(Name = "Body", Namespace = "http://sap.com/bw")]