码迷,mamicode.com
首页 > 其他好文 > 详细

遍历XML文件添加到TreeView递归调用

时间:2016-11-02 14:26:01      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:read   count()   nts   event   void   获取   component   add   cti   

 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);
                }
            }
        }
    }
}

 

遍历XML文件添加到TreeView递归调用

标签:read   count()   nts   event   void   获取   component   add   cti   

原文地址:http://www.cnblogs.com/blacop/p/6022478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!