C# System.Xml.Serialization Self-nested elements

给你一囗甜甜゛ 提交于 2019-12-04 12:34:01

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

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

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?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!