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.Windows.Forms.VisualStyles;
using System.Xml;
namespace 居委会
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//窗体起始调用读取到的XML文件方法
private void Form1_Load(object sender, EventArgs e)
{
Read();
}
//读取XML文件
public void Read()
{
XmlDataDocument doc = new XmlDataDocument();
//加载XML文件
doc.Load("Address.xml");
//获取根节点
XmlNode nodes = doc.DocumentElement;
//创建一个TreeNode对象
TreeNode tn = new TreeNode();
//TreeView对象的文本将显示为根节点的文本
tn.Text = nodes.Attributes["name"].InnerText;
//添加到TreeView控件上
tvList.Nodes.Add(tn);
TreeNode tns = null;
TreeNode childs = null;
//遍历从XML中读取到的信息
foreach (XmlNode item in nodes.ChildNodes)
{
tns = new TreeNode();
tns.Text = item.Attributes["name"].InnerText;
tns.Tag = item;
TreeNode child = null;
foreach (XmlNode node in item.ChildNodes)
{
child = new TreeNode();
child.Text = node.Attributes["name"].InnerText;
foreach (XmlNode items in node.ChildNodes)
{
childs = new TreeNode();
childs.Text = items.InnerText;
child.Nodes.Add(childs);
}
tns.Nodes.Add(child);
}
tn.Nodes.Add(tns);
}
}
//TreeView显示数据
private void tvList_AfterSelect(object sender, TreeViewEventArgs e)
{
//判断节点值,如果节点为0,txtStreet的文本显示XML文件上的父节点
if (tvList.SelectedNode.Level ==0)
{
txtStreet.Text = tvList.SelectedNode.Text;
}
//如果节点为1,txtJuWei文本显示XML文件的父节点的下一层
if (tvList.SelectedNode.Level == 1)
{
txtStreet.Text = tvList.SelectedNode.Parent.Text;
txtJuWei.Text = tvList.SelectedNode.Text;
}
//同上。。。。。
if (tvList.SelectedNode.Level == 2)
{
txtStreet.Text = tvList.Nodes[0].Text;
txtJuWei.Text = tvList.SelectedNode.Parent.Text;
txtBudingNO.Text = tvList.SelectedNode.Text;
}
if (tvList.SelectedNode.Level == 3)
{
txtStreet.Text = tvList.Nodes[0].Text;
txtJuWei.Text = tvList.SelectedNode.Parent.Text;
txtBudingNO.Text = tvList.SelectedNode.Text;
txtRoomNO.Text = tvList.SelectedNode.Text;
}
}
}
}
街道管理
最新推荐文章于 2018-01-28 10:53:00 发布