C#的System.Xml.Serialization自我嵌套元素
问题描述:
我试图反序列化
<graph>
<node>
<node>
<node></node>
</node>
</node>
<node>
<node>
<node></node>
</node>
</node>
</graph>
与
with
[XmlRoot("graph")]
class graph
{
List<Node> _children = new List<node>();
[XmlElement("node")]
public Node[] node
{
get { return _children.ToArray(); }
set { foreach(Node n in value) children.add(n) }
};
}
class Node
{
List<Node> _children = new List<node>();
[XmlElement("node")]
public Node[] node
{
get { return _children.ToArray(); }
set { foreach(Node n in value) children.add(n) }
};
}
但它口口声声说对象没有创建,空引用遇到试图设置的儿童在节点。什么是上面的问题?
but it keeps saying object not created, null reference encountered when trying to set children nodes. What is wrong above?
在此先感谢〜
答
您问题是集合处理器(S),加上如果不为空:
You issue is in the set handler(s), add if not null:
set { if(value != null) foreach(Node n in value) children.add(n) }