我想创建树视图(父和子和孙子等),取决于xml文件

本文关键字:取决于 文件 xml 视图 创建 | 更新日期: 2023-09-27 18:04:23

我有一个xml文件,有200个节点,如:

<grand name="AAA"       id="1"      father="0"></grand>
<grand name="BBB"       id="2"      father="1"></grand> 
<grand name="CCC"       id="3"      father="1"></grand>  
<grand name="DDD"       id="4"      father="2"></grand>  
<grand name="EEE"       id="5"      father="2"></grand>  
<grand name="FFF"       id="6"      father="5"></grand>  
<grand name="GGG"       id="7"      father="5"></grand>  
<grand name="HHH"       id="8"      father="5"></grand>  
<grand name="III"       id="9"      father="6"></grand>  
<grand name="JJJ"       id="10"     father="7"></grand>

我想创建treeview,所以,节点的第一个节点树,它的父属性是"0",然后每个节点树有子节点树每个xmlnode,所以treeview有一个祖父,然后他有两个孩子,两个孩子也有孩子,最后一个孩子也有孩子,等等。现在我写c#代码:

public struct person
        {
            public string id;
            public string name;
            public string father;
            public string child;
        }
        XDocument xmldoc = XDocument.Parse(Fulltree.Properties.Resources.index);
        person[] arr = new person[200];
        TreeNode[] tn = new TreeNode[200];
public Form1()
        {
            InitializeComponent();
            setlist();
            createtree(); 
        }
        public void setlist()
        {
            int i = 0;
            while (i < 200) { 
            var str = (from n in xmldoc.Descendants("grand")
                                        where n.Attribute("id").Value == (i+1).ToString()
                                        select new
                                        {
                                            Name = n.Attribute("name").Value,
                                            Sex = n.Attribute("sex").Value,
                                            Status = n.Attribute("status").Value,
                                            Child = n.Attribute("child").Value,
                                            Id = n.Attribute("id").Value,
                                            Father = n.Attribute("father").Value
                                        }).First();
            {
                arr[i].id = str.Id.ToString();
                arr[i].child = str.Child.ToString();
                arr[i].name = str.Name.ToString();
                arr[i].father = str.Father.ToString();
                tn[i] = new TreeNode(str.Id);
                tn[i].Text = str.Name.ToString();
            }
            i+=1;
                           }// end while
            } //end setlist
        public void createtree()
        {
            AddAllChildren(arr[0], 0);
        }
  public  void AddAllChildren( person[] arr , int parentIndex ) {
    for (int childIndex = parentIndex+1; childIndex < arr.Length; ++childIndex)
            {
                if( arr[childIndex].father == arr[parentIndex].id ) {
                    tn[parentIndex].Nodes.Add(tn[childIndex]);
                  AddAllChildren( arr[childIndex], childIndex );
                                                                     }
              }
                                                              }

现在,当我运行这段代码时,我得到的消息是,我必须为AddallChild方法做stub,当我这样做时,我得到错误:(方法或操作未实现。)在第(1)行,down

private void AddAllChildren(person person, int p)
 {
        throw new NotImplementedException();    line(1)
 }

thanks for all

我想创建树视图(父和子和孙子等),取决于xml文件

看起来您意外地生成了另一个方法AddAllChildren。试着把它去掉

TreeNode构造器new TreeNode(str.Id);用Text = str.Id创建节点然后用:

覆盖它
tn[i].Text = str.Name.ToString();

你将在循环的每一步都遍历xml,而不是只做一次。

 public void setlist()
        {
            var allPeople = xmldoc.Descendants("grand").Select(n => new person
                                          {
                                              name = n.Attribute("name").Value,
                                              /*Sex = n.Attribute("sex").Value,   //never used
                                              Status = n.Attribute("status").Value, //never used*/
                                              child = n.Attribute("child").Value,
                                              id = n.Attribute("id").Value,
                                              father = n.Attribute("father").Value
                                          }).ToList();
            var rootTreeNode = GetTree(allPeople, "0").First();
            //do something with rootTreeNode....
        }
        private TreeNode[] GetTree(List<person> allPeople, string parent)
        {
            return allPeople.Where(p => p.father == parent).Select(p =>
            {
                var node = new TreeNode(p.name);
                node.Tag = p;
                node.Nodes.AddRange(GetTree(allPeople, p.id));
                return node;
            }).ToArray();
        }