为什么我的SOAP XML响应不会在C#中反序列化?

时间:2012-03-28 19:48:30

标签: c# xml soap

我的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));
        }
    }
}

2 个答案:

答案 0 :(得分:3)

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

你能试试吗?

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

答案 1 :(得分:0)

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

<?xml version="1.0"?>

另外,将XmlRoot属性与命名空间一起添加到AssetList类:

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