XML 反序列化:所有属性都为 null/不需要的 EndElement
本文关键字:null 不需要 EndElement 反序列化 属性 XML | 更新日期: 2023-09-27 18:30:52
感谢任何帮助,我一直在挣扎。
从 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; }
}
编辑: 如果省略数据协定 (名称 = "对象") ,则会出现此问题。如果添加了该数据合同属性,则会出现以下错误:
System.Runtime.Serialization.SerializationException: Error in line 1 position 157.
'EndElement' 'Object' from namespace '' is not expected.
Expecting element 'Something| Anything'.
我做错了什么?
从 CollectionDataContract 中删除ItemName
。重新添加 Result
类的数据协定,并使用空字符串向其添加 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>