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);
                    }
                }
            }
        }
    }
  • 相关阅读:
    手机APP远程空气质量监测应用
    SPI
    2017-10-14
    常量声明
    ios- nil NULL 和 NSNull
    Xcode搭建真机调试环境 图文实例
    ios notification
    集合对象总结
    集合对象(NSSet,NSMutableSet,NSIndexSet)
    词典对象(NSDictionary和NSMutableDictionary)
  • 原文地址:https://www.cnblogs.com/blacop/p/6022478.html
Copyright © 2011-2022 走看看