文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>递归XML加载WinForm Treeview

递归XML加载WinForm Treeview

时间:2011-04-08  来源:V587

XMLFile1.xml文件如下:

view source print?
01 <?xml version="1.0" encoding="utf-8" ?>
02 <root name="rootNode" text="根节点">
03   <node name="node1"  text="节点1">
04     <childNode name="childNode1" text="子节点1">
05       <grandchild name="grandchild1" text="子子节点1"></grandchild>
06       <grandchild name="grandchild2" text="子子节点2"></grandchild>
07       <grandchild name="grandchild3" text="子子节点3"></grandchild>
08       <grandchild name="grandchild4" text="子子节点4"></grandchild>
09     </childNode>
10     <childNode name="childNode1" text="子节点1"></childNode>
11     <childNode name="childNode2" text="子节点2"></childNode>
12   </node>
13   <node name="node2"  text="节点2">
14     <childNode name="childNode1" text="子节点1"></childNode>
15     <childNode name="childNode2" text="子节点2"></childNode>
16     <childNode name="childNode3" text="子节点3"></childNode>
17   </node>
18   <node name="node3"  text="节点3">
19     <childNode name="childNode1" text="子节点1"></childNode>
20     <childNode name="childNode2" text="子节点2"></childNode>
21     <childNode name="childNode3" text="子节点3"></childNode>
22   </node>
23   <node name="node4"  text="节点4">
24     <childNode name="childNode1" text="子节点1"></childNode>
25     <childNode name="childNode2" text="子节点2"></childNode>
26     <childNode name="childNode3" text="子节点3"></childNode>
27   </node>
28 </root>

将这个xml文件加载到Winform的treeview上

后台代码如下:

view source print?
01 using System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Drawing;
06 using System.Text;
07 using System.Windows.Forms;
08 using System.Xml;
09   
10 namespace WindowsApplication1
11 {
12     public partial class Form1 : Form
13     {
14         public Form1()
15         {
16             InitializeComponent();
17   
18             XmlDocument doc = new XmlDocument();
19   
20             try
21             {
22                 string path = AppDomain.CurrentDomain.BaseDirectory + "XMLFile1.xml";
23                 doc.Load(path);
24                 treeView1.BeginUpdate();
25                 XmlElement xe = (XmlElement)doc.SelectSingleNode("root");
26                 TreeNode root = new TreeNode();
27                 root.Name = xe.GetAttribute("name");
28                 root.Text = xe.GetAttribute("text");
29                 root = GetChildNodes(xe, root);
30                 treeView1.Nodes.Add(root);
31                 treeView1.EndUpdate();
32             }
33             catch (Exception ex)
34             {
35                 MessageBox.Show(ex.ToString());
36             }
37         }
38   
39         public TreeNode GetChildNodes(XmlElement xe, TreeNode upNode)
40         {
41             if (xe.HasChildNodes)
42             {
43                 foreach (XmlNode node in xe.ChildNodes)
44                 {
45                     XmlElement xlt = (XmlElement)node;
46                     TreeNode tn = new TreeNode();
47                     tn.Text = xlt.GetAttribute("text");
48                     tn.Name = xlt.GetAttribute("name");
49                     GetChildNodes(xlt, tn);
50                     upNode.Nodes.Add(tn);
51                 }
52             }
53             return upNode;
54         }
55     }
56 }

效果如下图:

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载