任何帮助都会受到赞赏,我一直在努力。
我从HTTP请求收到XML响应。我无法控制它的结构,所以我必须将该结构与我的数据契约相匹配。但是,无论我做什么,所有结果对象中的属性都是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'.
我做错了什么?
答案 0 :(得分:1)
从 CollectionDataContract 中删除ItemName
。为Result
类重新添加 DataContract ,并使用空字符串向其添加Namespace
属性。如果在xml结构中没有显式定义的命名空间.Net将假定一个空字符串作为命名空间,你必须自己绑定它。
[CollectionDataContract(Name = "Customnamedroot", Namespace = "")]
public class SearchResults : List<Result>
{
}
[DataContract(Name = "Object", Namespace = "")]
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; }
}
作为旁注;即使您不控制XML的生成,您仍然可以在将其反序列化为具体类之前使用Linq to Xml修改它的结构。这里只是我正在进行的转型的一个例子。通过转换XML,可以防止XML结构规定具体类的外观和组合方式,并且可以按照自己喜欢的方式自由地进行OOP。
protected string OnTransformXml(string xml)
{
var doc = XDocument.Parse(xml);
var root = doc.Descendants("root").FirstOrDefault();
var allResults = doc.Descendants("Result").ToList();
foreach (var node in allResults)
{
// remove all Result elements from their current location
node.Remove();
// rename the Result element to RawResult
node.Name = "RawResult";
// create a container for the Result elements to live in
var rawMembersElement = new XElement("Result");
// add each Result element to the new container
rawMembersElement.Add(node);
// add the new container
root.Add(rawMembersElement);
}
return doc.ToString();
}
改变这个:
<root>
<Result>
<a>1</a>
<b></b>
</Result>
</root>
到此:
<root>
<Result>
<RawResult>
<a>1</a>
<b></b>
</RawResult>
</Result>
</root>