C#解析 居委会的 XML 文件方法

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值