[英]DataContractSerializer does not deserialize collection of objects
Good evening, I have the following XML: 晚上好,我有以下XML:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<project xmlns="">
<title>MyProject</title>
<nodes>
<node>
<name>base</name>
<class>BaseNode</class>
</node>
<node>
<name>detail</name>
<class>DetailNode</class>
</node>
...
</nodes>
</project>
... that I'd wish to deserialize into the following object structure, so that a Project
contains a "Title" and a NodeCollection
(which in turn contains multiple Node
elements): ...我希望反序列化为以下对象结构,以便一个
Project
包含一个“ Title”和一个NodeCollection
(后者又包含多个Node
元素):
[DataContract(Name = "project")]
class Project
{
[DataMember(Name = "title")]
public string Title { get; set; }
[DataMember(Name = "nodes")]
public NodeCollection Nodes { get; set; }
}
[CollectionDataContract(Name = "nodes", ItemName = "node")]
class NodeCollection : List<Node>
{
}
[DataContract(Name = "node")]
class Node
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "class")]
public string Class { get; set; }
}
With this architecture, deserialization completes without errors, returning an expected Project
object, but: 使用此体系结构,反序列化可以顺利完成,并返回预期的
Project
对象,但是:
The Title property on the Project object is set to the expected value, but the NodeCollection is always empty. Project对象上的Title属性设置为期望值,但是NodeCollection始终为空。 In fact, it's not even initialized:
实际上,它甚至还没有初始化:
Object reference not set to an instance of an object
你调用的对象是空的
For some odd reason, the deserializer "does not recognize any nodes". 由于某种奇怪的原因,解串器“无法识别任何节点”。 I tried adding the
KnownType
attribute, no success. 我尝试添加
KnownType
属性,但没有成功。
What am I doing wrong? 我究竟做错了什么?
It works if you specify the Namespace
and Order
attributes; 如果您指定
Namespace
和Order
属性,它将起作用。 otherwise it complains about unexpected namespaces or fails to read the node(s) entirely, and for some reason it's expecting Title
to come after Nodes
. 否则,它将抱怨意外的名称空间或无法完全读取节点,并且由于某种原因,它期望
Title
Nodes
之后。
[DataContract(Name = "project", Namespace="")]
public class Project
{
[DataMember(Name = "title",Order=1)]
public string Title { get; set; }
[DataMember(Name = "nodes", Order=2)]
public NodeCollection Nodes { get; set; }
}
[CollectionDataContract(Name = "nodes",Namespace="", ItemName = "node")]
public class NodeCollection : List<Node> { }
[DataContract(Name = "node", Namespace="")]
public class Node
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "class")]
public string Class { get; set; }
}
Looking at the documentation , 查看文档 ,
The basic rules for data ordering include:
数据排序的基本规则包括:
- If a data contract type is a part of an inheritance hierarchy, data members of its base types are always first in the order.
如果数据协定类型是继承层次结构的一部分,则其基本类型的数据成员始终按顺序排列。
- Next in order are the current type's data members that do not have the Order property of the DataMemberAttribute attribute set, in alphabetical order.
接下来的是当前类型的数据成员,这些成员没有按字母顺序设置DataMemberAttribute属性的Order属性。
- Next are any data members that have the Order property of the DataMemberAttribute attribute set.
接下来是设置了DataMemberAttribute属性的Order属性的所有数据成员。 These are ordered by the value of the Order property first and then alphabetically if there is more than one member of a certain Order value.
这些按先后按Order属性的值排序,如果某个Order值中有多个成员,则按字母顺序排序。 Order values may be skipped.
订单值可能会被跳过。
Because "N" comes before "T" alphabetically, it was expecting NodeCollection
to be first. 因为“ N”按字母顺序排在“ T”之前,所以它期望
NodeCollection
是第一个。 Even though things are "out of order", it will still deserialize (which seems strange, but that's how it works) - it just will fill those objects/members with null. 即使事情“乱序”,它仍然会反序列化(这看起来很奇怪,但这就是它的工作原理)-它只会用null填充那些对象/成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.