C#的System.Xml.Serialization自我嵌套元素 [英] C# System.Xml.Serialization Self-nested elements

查看:117
本文介绍了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) }

这篇关于C#的System.Xml.Serialization自我嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆