Treeview XML创建

时间:2019-01-14 13:46:28

标签: c# xml

我有以下无法解决的问题,我试图基于以下xml中包含的数据创建树视图:

<folder id="1" name="folder1" />
<folder id="2" name="folder2" />
<folder id="3" name="folder3" />
<folder id="4" name="folder4" />
<folder id="5" name="folder5" />
<folder id="6" name="folder6" parent_id="2"/>
<folder id="7" name="folder7" parent_id="3"/>
<folder id="8" name="folder8" parent_id="6"/>
<folder id="9" name="folder9" parent_id="5"/>

我使用的代码是:

public void Folder()
        {

            XmlDocument dom = new XmlDocument();
            XmlNodeList xmlnode;
            dom.Load(xmlPath);
            xmlnode = dom.GetElementsByTagName("folder");

            string FolderName;
            int? ParentFolder;
            int FolderID;
            string _subnode;
            treeView2.Nodes.Clear();

            foreach(XmlNode node in xmlnode)
            {
                FolderName = GetAttributeText(node, "name");
                FolderID = Int32.Parse(GetAttributeText(node,"id"));
                ParentFolder = ParseNullableInt(GetAttributeText(node, "parent_id"));

                TreeNode subnode = new TreeNode(FolderName);
                TreeNode treeNode = new TreeNode(FolderName);

                if (ParentFolder.HasValue)
                {


                    treeView2.Nodes[(int)ParentFolder].Nodes.Add("Cartelle", FolderName);



                }
                else
                {

                    treeView2.Nodes.Add(treeNode);


                }

问题是,如果一个孩子是一个打结的孩子,我会被误认为是错误的,并且我不会冒生一个孩子的风险。

0 个答案:

没有答案