C# Winform工程省市县三级行政区联动1 --XML

更新:把控件ID改了一下


C#窗体应用程序,省市县三级行政区ComboBox控件,联动下拉选择。

运行效果



三级行政区XML文件链接:http://pan.baidu.com/s/1kUEBdtH


参考文献:

http://www.cnblogs.com/Hisin/archive/2012/02/27/2370646.html

http://blog.csdn.net/make1828/article/details/40108177

<pre name="code" class="csharp">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.Web;
using System.Xml;
using System.Runtime.Serialization;
using System.IO;
using System.Net;

namespace MyTest
{
    public partial class Form2 : Form
    {
        public XmlDocument Provincedoc;
        public XmlDocument Citydoc;
        public XmlDocument Districtdoc;

        public XmlElement ProvincerootElem;
        public XmlElement CityrootElem;
        public XmlElement DistrictrootElem;

        public XmlNodeList ProvinceNodes;
        public XmlNodeList CityNodes;
        public XmlNodeList DistrictNodes;

        public string ProvinceName;
        public string ProvinceID;
        public string CityName;
        public string CityID;

        public Form2()
        {
            InitializeComponent();

            Provincedoc = new XmlDocument();
            Provincedoc.Load(System.Windows.Forms.Application.StartupPath + "/RegionFile/Provinces.xml");    //加载Xml文件  
            ProvincerootElem = Provincedoc.DocumentElement;   //获取根节点  
            ProvinceNodes = ProvincerootElem.GetElementsByTagName("Province"); //获取Provinces子节点集合

            Citydoc = new XmlDocument();
            Citydoc.Load(System.Windows.Forms.Application.StartupPath + "/RegionFile/Cities.xml");    //加载Xml文件  
            CityrootElem = Citydoc.DocumentElement;   //获取根节点  
            CityNodes = CityrootElem.GetElementsByTagName("City"); //获取Cities子节点集合 

            Districtdoc = new XmlDocument();
            Districtdoc.Load(System.Windows.Forms.Application.StartupPath + "/RegionFile/Districts.xml");    //加载Xml文件  
            DistrictrootElem = Districtdoc.DocumentElement;   //获取根节点  
            DistrictNodes = DistrictrootElem.GetElementsByTagName("District"); //获取Cities子节点集合 

            foreach (XmlNode Provincenode in ProvinceNodes)
            {
                string strProvinceName = ((XmlElement)Provincenode).GetAttribute("ProvinceName");   //获取name属性值  
                string strProvinceID = ((XmlElement)Provincenode).GetAttribute("ID");
                comboBox_sheng.Items.Add(strProvinceName);
            }
        }

        private void comboBox_sheng_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProvinceName = comboBox_sheng.SelectedItem.ToString();
            comboBox_xian.Text = "";
            comboBox_shi.Text = "";
            comboBox_xian.Items.Clear();
            comboBox_shi.Items.Clear();
            foreach (XmlNode Provincenode in ProvinceNodes)
            {
                if (((XmlElement)Provincenode).GetAttribute("ProvinceName").Equals(comboBox_sheng.SelectedItem.ToString()))
                {
                    ProvinceID = ((XmlElement)Provincenode).GetAttribute("ID");
                    break;
                }
            }
            foreach (XmlNode Citynode in CityNodes)
            {
                if (((XmlElement)Citynode).GetAttribute("PID").Equals(ProvinceID))
                {
                    string strCityName = ((XmlElement)Citynode).GetAttribute("CityName");
                    comboBox_shi.Items.Add(strCityName);
                }
            }
            comboBox_shi.SelectedItem = comboBox_shi.Items[0];
        }

        private void comboBox_shi_SelectedIndexChanged(object sender, EventArgs e)
        {
            CityName = comboBox_shi.SelectedItem.ToString();
            comboBox_xian.Text = "";
            comboBox_xian.Items.Clear();
            foreach (XmlNode Citynode in CityNodes)
            {
                if (((XmlElement)Citynode).GetAttribute("CityName").Equals(CityName) && ((XmlElement)Citynode).GetAttribute("PID").Equals(ProvinceID))
                {
                    CityID = ((XmlElement)Citynode).GetAttribute("ID");
                    break;
                }
            }
            foreach (XmlNode Districtnode in DistrictNodes)
            {
                if (((XmlElement)Districtnode).GetAttribute("CID").Equals(CityID))
                {
                    string strDistrictName = ((XmlElement)Districtnode).GetAttribute("DistrictName");   //获取name属性值  
                    comboBox_xian.Items.Add(strDistrictName);
                }
            }
            comboBox_xian.SelectedItem = comboBox_xian.Items[0];
        }

        private void comboBox_xian_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

 


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值