winform省市县级联

来源:互联网 发布:淘宝的帐号管理在哪 编辑:程序博客网 时间:2024/04/30 22:11

读取xml文件加载到combox中     

XmlDocument doc = new XmlDocument();

        /// <summary>
        /// 省市级联
        /// </summary>
        private void init()
        {
          
            doc.Load(Command.GetApplicationPath()+"省市区.xml");
            XmlNode provinces = doc.SelectSingleNode("/ProvinceCity");
            foreach (XmlNode province in provinces.ChildNodes)
            {
                tscboxPro.Items.Add(province.Name);


            }
            tscboxPro.SelectedIndex = 0;

        }


  private void tscboxPro_SelectedIndexChanged(object sender, EventArgs e)
        {
            tscboxCity.Items.Clear();
            string xpath = string.Format("/ProvinceCity/{0}/City", tscboxPro.SelectedItem.ToString());


            XmlNodeList cities = doc.SelectNodes(xpath);


            foreach (XmlNode city in cities)
            {
                tscboxCity.Items.Add(city.Attributes["Name"].Value);
            }
            if (tscboxCity.Items.Count >= 0)
            {
                tscboxCity.SelectedIndex = 0;
            }
        }
        private void tscboxCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbxCityArea.Items.Clear();
            string xpath = string.Format("/ProvinceCity/{0}/City[@Name='{1}']/CityArea",


                    tscboxPro.SelectedItem.ToString(),


                    tscboxCity.SelectedItem.ToString());
            XmlNodeList CityAreas = doc.SelectNodes(xpath);
            foreach (XmlNode area in CityAreas)
            {


                cbxCityArea.Items.Add(area.Attributes["Name"].Value);


            }
            if (cbxCityArea.Items.Count > 0)
            {
                cbxCityArea.SelectedIndex = 0;
            }
        }



0 0
原创粉丝点击