2012-03-28 53 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