2012-03-28 56 views
1

我SOAP服務調用返回以下響應:爲什麼我的SOAP XML響應在C#中反序列化?

<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對象我試圖反序列化是通過更新我的服務引用來針對相同的確切服務I自動生成的我正在用我的GET請求進行呼叫。

這是爲什麼會失敗?

的資產列表類的產生:

[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)); 
     } 
    } 
} 
+0

您可以添加自動生成的AssetList類嗎? – 2012-03-28 19:50:23

+0

更新了問題與類 – KallDrexx 2012-03-28 19:51:53

回答

3

我認爲問題與nam有關ESPACES。

你可以試試嗎?

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

明白了!非常感謝 – KallDrexx 2012-03-28 20:12:46

0

嘗試添加這是在你的XML的第一行:

<?xml version="1.0"?> 

此外,添加XmlRoot屬性與命名空間到你的AssetList類:

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

不,我仍然在同一行(資產清單預計不會出現相同的錯誤) – KallDrexx 2012-03-28 19:59:52