gpt4 book ai didi

c# - XML反序列化: All properties null/EndElement not expected

转载 作者:行者123 更新时间:2023-12-02 13:15:37 25 4
gpt4 key购买 nike

感谢任何帮助,我一直在努力。

我从 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'.

我做错了什么?

最佳答案

从 CollectionDataContract 中删除 ItemName。为 Result 类重新添加 DataContract,并向其中添加带有空字符串的 Namespace 属性。如果 xml 结构中没有明确定义 namespace ,.Net 将假定一个空字符串作为 namespace ,您必须将自己绑定(bind)到该 namespace 。

[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>

关于c# - XML反序列化: All properties null/EndElement not expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18721786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com