zoukankan      html  css  js  c++  java
  • XML文件设置树形结构(无限级)原创

    在写Windows环境下,常会用到Tree控件,以下是通过XML文件构建Tree控件树形的方法.

    添加一个引用

    using System.XML.Linq;

    Code
    public static void LoadTree(TreeView tv)
            {
                tv.Nodes.Clear();
                XDocument doc 
    = XDocument.Load(SaveData.xmlPath);
                IEnumerable
    <XElement> q = from p in doc.Element("nodeList").Elements("node")
                        select p;
                
                
    foreach (var n in q)
                {
                    TreeNode node
    =new TreeNode();
                    
    string name=n.Attribute("name").Value;
                    node.Text 
    =name;
                    tv.Nodes.Add(node);
                    childTree(node, q.Where(c 
    => c.Attribute("name").Value == name));
                }
            }

            
    private static void childTree(TreeNode node, IEnumerable<XElement> x)
            {
                var q 
    = from p in x.Elements("childNode")
                        select p;
                
    foreach (var n in q)
                {
                    TreeNode tn 
    = new TreeNode();
                    
    string name = n.Attribute("name").Value;
                    tn.Text 
    = name;
                    node.Nodes.Add(tn);
                    childTree(tn, q.Where(c 
    => c.Attribute("name").Value == name));

                }
            }

    ---------------------------------------龙亿-----原创---------------------------------------------------------

  • 相关阅读:
    牛客 公式字符串求值
    牛客 括号字符串的有效性和最长有效长度
    POJ-2533 Longest Ordered Subsequence ( DP )
    HDU-1160 FatMouse's Speed ( DP )
    HDU-1260 Tickets ( DP )
    HDU-1074 Doing Homework( 状压DP )
    HDU-1069 Monkey and Banana ( DP )
    HDU-1087 Super Jumping! Jumping! Jumping!( DP )
    HDU-3746 Cyclic Nacklace ( kmp )
    HDU-2087 剪花布条 ( kmp )
  • 原文地址:https://www.cnblogs.com/pbuilder/p/1432091.html
Copyright © 2011-2022 走看看