对此的任何帮助表示赞赏,我一直在努力。
从 HTTP 请求中,我收到一个 XML 响应。我无法控制它的结构,因此我必须将该结构与我的数据合同相匹配。但是,无论我做什么, Result 对象中的所有属性都是 null 或默认值。
XML结构:
<Customnamedroot xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Results="2" Skip="0">
<Object>
<Something xsi:nil="true" />
<Anything>2012-06-08T11:21:27</Anything>
<Booleanthing>true</Booleanthing>
</Object>
<Object>
<Something>1463387</Something>
<Anything>2012-06-07T09:16:11.41</Anything>
<Booleanthing>true</Booleanthing>
</Object>
</Customnamedroot>
来电:
SearchResults objects = response.Content.ReadAsAsync<SearchResults>().Result;
对应的数据契约类:
[CollectionDataContract(Name = "Customnamedroot", ItemName = "Object", Namespace = "")]
public class SearchResults : List<Result>
{
}
//[DataContract(Name = "Object")]
public class Result
{
[DataMember(Order = 1, Name = "Something", IsRequired = false)]
public decimal? Something{ get; set; }
[DataMember(Order = 2, Name = "Anything", IsRequired = true, EmitDefaultValue = false)]
public DateTime Anything{ get; set; }
[DataMember(Order = 3, Name = "Booleanthing", IsRequired = true, EmitDefaultValue = false)]
public bool Booleanthing{ get; set; }
}
编辑:如果省略 DataContract(Name = "Object"),则会出现此问题。如果添加了该 DataContractAttribute,我会收到以下错误:
System.Runtime.Serialization.SerializationException: Error in line 1 position 157.
'EndElement' 'Object' from namespace '' is not expected.
Expecting element 'Something| Anything'.
我究竟做错了什么?