zoukankan      html  css  js  c++  java
  • 遍历XML文件添加到TreeView递归调用

     using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Xml.Linq;
    
    namespace 遍历XML文件添加到TreeView递归调用
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //读取xml文件
                XDocument xdoc = XDocument.Load("11.xml");
                XElement root = xdoc.Root;//获取根元素
                TreeNode tn = tv.Nodes.Add(root.Name.ToString());//根元素的名字显示到控件上
                LoadXElement(root, tn);
    
            }
    
            private void LoadXElement(XElement root, TreeNode tn)
            {
                foreach(XElement item in root.Elements())//遍历根元素下所有的子元素
                {
                    //判断当前的元素下是否还有元素
                    if(item.Elements().Count()>0)
                    {
                        TreeNode tn1 = tn.Nodes.Add(item.Name.ToString());
                        LoadXElement(item, tn1);//递归调用
                    }
                    else
                    {
                        tn.Nodes.Add(item.Value);
                    }
                }
            }
        }
    }
  • 相关阅读:
    启动Docker容器
    Docker 删除容器
    11.18数据库认证
    10.17权限认证
    9.16角色认证
    8.13数据库认证
    6.11Realm简介
    5.8认证流程分析
    4.7固定信息认证
    20张图表达程序员的心酸
  • 原文地址:https://www.cnblogs.com/blacop/p/6022478.html
Copyright © 2011-2022 走看看