为什么获胜';t我的SOAP XML响应在C#中反序列化

本文关键字:响应 XML 反序列化 SOAP 我的 获胜 为什么 | 更新日期: 2023-09-27 18:29:14

我的soap服务调用返回以下响应:

<AssetList xmlns="http://schemas.datacontract.org/2004/07/XOSDigital.Assets" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Facets xmlns:a="http://schemas.datacontract.org/2004/07/XOSDigital.Business_Classes.Search"/>
  <Results/>
  <err i:nil="true"/>
  <offset>0</offset>
  <total>0</total>
</AssetList>

当我试图通过串行化时

        AssetList assets;
        var serializer = new XmlSerializer(typeof(AssetList));
        assets = (AssetList)serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(response.Content)));

我得到以下异常:

<AssetList xmlns='http://schemas.datacontract.org/2004/07/XOSDigital.Assets'> was not expected.

我试图反序列化到的AssetList对象是通过根据我用GET请求调用的同一服务更新我的服务引用而自动生成的。

为什么会失败?

AssetList类生成为:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="AssetList", Namespace="http://schemas.datacontract.org/2004/07/XOSDigital.Assets")]
[System.SerializableAttribute()]
public partial class AssetList : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
    [System.NonSerializedAttribute()]
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private XOSDigital.XOSSuperfanAdminList.XosAssetWebService.AvailableFacetGroup[] FacetsField;
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private XOSDigital.XOSSuperfanAdminList.XosAssetWebService.Asset[] ResultsField;
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string errField;
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private int offsetField;
    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private int totalField;
    [global::System.ComponentModel.BrowsableAttribute(false)]
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
        get {
            return this.extensionDataField;
        }
        set {
            this.extensionDataField = value;
        }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    public XOSDigital.XOSSuperfanAdminList.XosAssetWebService.AvailableFacetGroup[] Facets {
        get {
            return this.FacetsField;
        }
        set {
            if ((object.ReferenceEquals(this.FacetsField, value) != true)) {
                this.FacetsField = value;
                this.RaisePropertyChanged("Facets");
            }
        }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    public XOSDigital.XOSSuperfanAdminList.XosAssetWebService.Asset[] Results {
        get {
            return this.ResultsField;
        }
        set {
            if ((object.ReferenceEquals(this.ResultsField, value) != true)) {
                this.ResultsField = value;
                this.RaisePropertyChanged("Results");
            }
        }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    public string err {
        get {
            return this.errField;
        }
        set {
            if ((object.ReferenceEquals(this.errField, value) != true)) {
                this.errField = value;
                this.RaisePropertyChanged("err");
            }
        }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    public int offset {
        get {
            return this.offsetField;
        }
        set {
            if ((this.offsetField.Equals(value) != true)) {
                this.offsetField = value;
                this.RaisePropertyChanged("offset");
            }
        }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    public int total {
        get {
            return this.totalField;
        }
        set {
            if ((this.totalField.Equals(value) != true)) {
                this.totalField = value;
                this.RaisePropertyChanged("total");
            }
        }
    }
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

为什么获胜';t我的SOAP XML响应在C#中反序列化

我认为问题与名称空间有关。

你能试试这个吗?

var serializer = new XmlSerializer(typeof(AssetList),"http://schemas.datacontract.org/2004/07/XOSDigital.Assets");

尝试将其添加为xml中的第一行:

<?xml version="1.0"?>

另外,将带有名称空间的XmlRoot属性添加到AssetList类:

[XmlRoot("AssetList", Namespace = "http://schemas.datacontract.org/2004/07/XOSDigital.Assets")]