比较简单,用到三个控件button treeview openFileDialog 上代码,关键技术:递归
form
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Xml;
10
11
12 namespace WindowsFormsApplication4
13 {
14 public partial class Form1 : Form
15 {
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 private void button1_Click(object sender, EventArgs e)
22 {
23 //从XML中读取数据到TreeView
24 if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
25 {
26 XmlDocument xmlDoc = new XmlDocument();
27 xmlDoc.Load(openFileDialog1.FileName);
28
29 XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes;
30
31 this.treeView1.BeginUpdate();
32 this.treeView1.Nodes.Clear();
33 XmlNode2TreeNode(xmlNodes, this.treeView1.Nodes);
34 this.treeView1.EndUpdate();
35 }
36 }
37
38
39 private void XmlNode2TreeNode(XmlNodeList xmlNode, TreeNodeCollection treeNode)
40 {
41 foreach (XmlNode var in xmlNode)
42 {
43 if (var.NodeType != XmlNodeType.Element)
44 {
45 continue;
46 }
47 TreeNode newTreeNode = new TreeNode();
48 if (var.Attributes.Count == 2)
49 {
50 newTreeNode.Text = var.Attributes[0].Name + "=" + var.Attributes[0].Value
51 + " " + var.Attributes[1].Name + "=" + var.Attributes[1].Value;
52 }
53 else
54 {
55 newTreeNode.Text = var.Attributes[0].Name + "=" + var.Attributes[0].Value
56 + " " + var.Attributes[1].Name + "=" + var.Attributes[1].Value
57 + " " + var.Attributes[2].Name + "=" + var.Attributes[2].Value;
58 }
59
60
61 if (var.HasChildNodes)
62 {
63 if (var.ChildNodes[0].NodeType == XmlNodeType.CDATA)
64 {
65 newTreeNode.Tag = var.ChildNodes[0].Value;
66 }
67
68 XmlNode2TreeNode(var.ChildNodes, newTreeNode.Nodes);
69 }
70 treeNode.Add(newTreeNode);
71 }
72 }
73 }
74 }
XML代码:
XML文档
<?xml version="1.0" encoding="utf-8"?>
<result_point name="Point" kernelType="any">
<XDO name="DataSet" kernelType="any">
<XDO name="OBJECTID" kernelType="int|list" value="1,2,3,4" />
<XDO name="Shape" kernelType="vector2d|list" value="113.80643,29.261196" />
<XDO name="AREA" kernelType="float|list" value="0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0," />
<XDO name="PERIMETER" kernelType="float|list" value="0,0,0" />
<XDO name="CNTYPT_" kernelType="float|list" value="1846,1791" />
<XDO name="CNTYPT_ID" kernelType="float|list" value="1666,3,200,471,74,1757" />
<XDO name="NAME" kernelType="string|list" value="通城县,来凤县,崇阳县" />
<XDO name="PYNAME" kernelType="string|list" value="Tongcheng Xian,Laifeng Xian" />
<XDO name="CLASS" kernelType="string|list" value="AI,AI,AI,AI,AI,AH,AH,AH,AI,AI" />
<XDO name="ID" kernelType="int|list" value="2632,2199,2300,2295,2186,2580" />
<XDO name="PN" kernelType="int|list" value="1,1,1,1,1,1,1,1,1,1,1,1,1,1,1" />
<XDO name="ADCODE93" kernelType="int|list" value="422324,422827,422325,422326,422826" />
<XDO name="CS_Check" kernelType="string|list" value="True,True,True" />
<XDO name="CS_Flag" kernelType="int|list" value="0,0,0,0,1,0,0" />
</XDO>
</result_point>