C# System.Xml.Serialization 自嵌套元素

发布于 2024-09-01 12:17:56 字数 819 浏览 8 评论 0原文

我正在尝试反序列化

<graph>
<node>
   <node>
     <node></node>
   </node>
</node>
<node>
   <node>
     <node></node>
   </node>
</node>
</graph>

[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) }
   };
}

但它一直说对象未创建,尝试设置子节点时遇到空引用。上面有什么问题吗?

先谢谢啦~

I am trying to deserialize

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

Thanks in advance~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-09-08 12:17:56

您的问题是在集合处理程序中,如果不为空则添加:

set { if(value != null) foreach(Node n in value) children.add(n) }

You issue is in the set handler(s), add if not null:

set { if(value != null) foreach(Node n in value) children.add(n) }
懒的傷心 2024-09-08 12:17:56

我无法重现你的错误。我使用了以下代码:

string xml = @"<graph>
<node>
   <node>
     <node></node>
   </node>
</node>
<node>
   <node>
     <node></node>
   </node>
</node>
</graph>";

[XmlRoot("graph")]
public class graph
{
    [XmlElement("node")]
    public Node[] node;
}

public class Node
{
    [XmlElement("node")]
    public Node[] children;
}

XmlSerializer serializer = new XmlSerializer(typeof(graph));

using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream))
{
    writer.Write(xml.Replace(Environment.NewLine, String.Empty));
    writer.Flush();
    stream.Position = 0;

    var graph = serializer.Deserialize(stream) as graph;
}

您可以发布您用来反序列化的内容吗?

I can't reproduce your error. I used the following code:

string xml = @"<graph>
<node>
   <node>
     <node></node>
   </node>
</node>
<node>
   <node>
     <node></node>
   </node>
</node>
</graph>";

[XmlRoot("graph")]
public class graph
{
    [XmlElement("node")]
    public Node[] node;
}

public class Node
{
    [XmlElement("node")]
    public Node[] children;
}

XmlSerializer serializer = new XmlSerializer(typeof(graph));

using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream))
{
    writer.Write(xml.Replace(Environment.NewLine, String.Empty));
    writer.Flush();
    stream.Position = 0;

    var graph = serializer.Deserialize(stream) as graph;
}

Can you post what you're using to deserialize?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文