[英]Why won't my SOAP XML response deserialize in C#?
My soap service call is returning the following response: 我的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>
When I attempt to serialize this via: 当我尝试通过以下方式序列化时:
AssetList assets;
var serializer = new XmlSerializer(typeof(AssetList));
assets = (AssetList)serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(response.Content)));
I get the following exception: 我得到以下异常:
<AssetList xmlns='http://schemas.datacontract.org/2004/07/XOSDigital.Assets'> was not expected.
The AssetList
object I am trying to deserialize to was automatically generated by updating my service reference against the same exact service I am calling with my GET request. 我尝试反序列化的
AssetList
对象是通过使用我的GET请求更新我的服务引用来自动生成的。
Why is this failing? 为什么这会失败?
The AssetList class is generated as: 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));
}
}
}
I think the problem is related with namespaces. 我认为这个问题与命名空间有关。
Can you try this? 你能试试吗?
var serializer = new XmlSerializer(typeof(AssetList),"http://schemas.datacontract.org/2004/07/XOSDigital.Assets");
Try to add this as the first line in your xml: 尝试将其添加为xml中的第一行:
<?xml version="1.0"?>
Also, add XmlRoot
attribute with namespace to your AssetList
class: 另外,将带有命名空间的
XmlRoot
属性添加到AssetList
类:
[XmlRoot("AssetList", Namespace = "http://schemas.datacontract.org/2004/07/XOSDigital.Assets")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.