【问题标题】:Outputting NTree object to XML.将 NTree 对象输出到 XML。
【发布时间】:2012-08-29 10:09:19
【问题描述】:

我正在尝试按照Tree data structure in C# 实现以下树结构类

delegate void TreeVisitor<T>(T nodeData);

class NTree<T>
{
    T data;
    LinkedList<NTree<T>> children;

    public NTree(T data)
    {
        this.data = data;
        children = new LinkedList<NTree<T>>();
    }

    public void addChild(T data)
    {
        children.AddFirst(new NTree<T>(data));
    }

    public NTree<T> getChild(int i)
    {
        foreach (NTree<T> n in children)
            if (--i == 0) return n;
        return null;
    }

    public NTree<T> getChild( T data )
{
    foreach (NTree<T> n in children)
        {
         if (n.data.Equals(data))
     {
         return n;
     }
    }
    return null;
    }

    public void traverse(NTree<T> node, TreeVisitor<T> visitor)
    {
        visitor(node.data);
        foreach (NTree<T> kid in node.children)
            traverse(kid, visitor);
    }        
}

然后我想将对象的实例输出为 XML,但是我正在努力保持子/父层次结构到位

目前为止

NTree<string> root = new NTree<string>( "Electronic" );
root.addChild( "Techno" );
root.getChild( "Techno" ).addChild( "Detroit" );
root.getChild( "Techno" ).addChild( "Gabba" );
root.addChild( "House" );
root.getChild( "House" ).addChild( "Deep" );
root.getChild( "House" ).addChild( "Ambient" );
root.getChild( "House" ).addChild( "Chicago" );

XElement treeAsXml = new XElement("Root");
root.traverse( root, new TreeVisitor<string>( this.ConvertNodeToXml ) );

private void ConvertNodeToXml( string nodeData )
{
 XElement node = new XElement( "Node" );
 node.Value = nodeData;
 this.treeAsXml.Add( node );
}

这只是给了我:

<Root><Node>Electronic</Node><Node>House</Node><Node>Chicago</Node><Node>Ambient</Node><Node>Deep</Node><Node>Techno</Node><Node>Gabba</Node><Node>Detroit</Node></Root>

我怎样才能正确输出这个,理想情况如下

<Node value="Electronic">
<Node value="Techno">
    <Node value="Detroit" />
    <Node value="Gabba" />
</Node> 
<Node value="House">
    <Node value="Deep" />
    <Node value="Ambient" />
    <Node value="Chicago" />
</Node>     
</Node>

【问题讨论】:

    标签: c# xml-serialization c#-3.0 linq-to-xml


    【解决方案1】:

    问题是您的委托没有上下文,它不知道自己是父级,也不知道自己是子级,因为它只获取节点的数据。您要么必须更改委托以包含某种类型的上下文,要么可以将其作为扩展方法:

    public static class NTreeXmlHelper
    {
        public static XElement TreeAsXml<T>(this NTree<T> node)
        {
            XElement element = new XElement("Node",
                                      new XAttribute("value", node.data));
    
            foreach (var child in node.children)
            {
                element.Add(TreeAsXml(child));
            }
    
            return element;
        }
    }
    

    为此,您必须公开 childrendata 属性。

    要使用它,只需这样做:

            NTree<string> root = new NTree<string>("Electronic");
            root.addChild("Techno");
            root.getChild("Techno").addChild("Detroit");
            root.getChild("Techno").addChild("Gabba");
            root.addChild("House");
            root.getChild("House").addChild("Deep");
            root.getChild("House").addChild("Ambient");
            root.getChild("House").addChild("Chicago");
    
            treeAsXml = root.TreeAsXml();
            Console.WriteLine(treeAsXml.Tostring());
    

    【讨论】:

    • 这仍然只是将每个节点添加到树的根级别。我需要一种能够维护层次结构的方法
    • 我改了答案,看看
    • 绝对成功。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2010-10-08
    • 2016-08-17
    • 2018-01-01
    • 1970-01-01
    相关资源
    最近更新 更多