using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace 街道管理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
xianshi();
}
private void xianshi()
{
//第一步 找 子节点
XmlDocument doc = new XmlDocument();
doc.Load("Address.xml");
XmlNode nodes = doc.DocumentElement;
//TreeView 控件
TreeNode tn = new TreeNode();
tn.Text = nodes.Attributes["name"].InnerText;
treeView1.Nodes.Add(tn);
//开始 第二步 找根节点
foreach (XmlNode item in nodes.ChildNodes)
{
TreeNode tn2 = new TreeNode();
tn2.Text = item.Attributes["name"].InnerText;
tn.Nodes.Add(tn2);
foreach (XmlNode item2 in item.ChildNodes)
{
TreeNode tn3 = new TreeNode();
tn3.Text = item2.Attributes["name"].InnerText;
tn2.Nodes.Add(tn3);
foreach (XmlNode item3 in item2.ChildNodes)
{
TreeNode tn4 = new TreeNode();
tn4.Text = item3.InnerText;
tn3.Nodes.Add(tn4);
}
}
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeView1.SelectedNode.Level == 0)
{
this.textBox1.Text = treeView1.SelectedNode.Text;
}
if (treeView1.SelectedNode.Level == 1)
{
this.textBox1.Text = treeView1.SelectedNode.Parent.Text;
this.textBox2.Text = treeView1.SelectedNode.Text;
}
if (treeView1.SelectedNode.Level == 2)
{
this.textBox1.Text = treeView1.Nodes[0].Text;
this.textBox2.Text = treeView1.SelectedNode.Parent.Text;
this.textBox3.Text = treeView1.SelectedNode.Text;
}
if (treeView1.SelectedNode.Level == 3)
{
this.textBox1.Text = treeView1.Nodes[0].Text;
this.textBox2.Text = treeView1.SelectedNode.Parent.Parent.Text;
this.textBox3.Text = treeView1.SelectedNode.Parent.Text;
this.textBox4.Text = treeView1.SelectedNode.Text;
}
<?xml version="1.0" encoding="gb2312"?>
<root name="石牌街道">
<juwei name="瑞华居委">
<jianzhu smid="6" name="瑞华花园1栋">
<fjname>101室</fjname>
<fjname>102室</fjname>
</jianzhu>
<jianzhu smid="7" name="瑞华花园2栋">
<fjname>101室</fjname>
<fjname>102室</fjname>
</jianzhu>
</juwei>
<juwei name="金帝居委">
<jianzhu smid="4" name="金帝花园1栋">
<fjname>101室</fjname>
<fjname>102室</fjname>
</jianzhu>
</juwei>
<juwei name="朝阳居委">
<jianzhu smid="5" name="朝阳花园1栋">
<fjname>101室</fjname>
<fjname>102室</fjname>
</jianzhu>
</juwei>
</root>