用xml数据实现comboBox级联的方法

本文介绍了一个用于从XML配置文件中读取国家、省份和城市信息的方法。通过使用C#编程语言,该方法能够动态填充ComboBox控件,实现地区信息的选择功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  #region 字段定义

        private string _ConfigPath = string.Empty;

        #endregion

        #region 属性定义

        /// <summary>         /// 配置文件路径         /// </summary>         public string ConfigPath         {             get { return this._ConfigPath; }             set { this._ConfigPath = value; }         }

        #endregion

        #region 构造函数

        public AreaConfig()         {

        }

        public AreaConfig(string pConfigPath)         {             this._ConfigPath = pConfigPath;         }

        #endregion

        #region 方法

        public void GetCountry(ComboBox pcobCountry)         {             XmlDocument xmlDoc = new XmlDocument();             xmlDoc.Load(this._ConfigPath);             XmlNodeList elemList = xmlDoc.SelectSingleNode("AreaInfo").ChildNodes;             for (int i = 0; i < elemList.Count; i++)             {                 ComboBoxItem cobItem = new ComboBoxItem();                 cobItem.Content = elemList[i].Attributes["name"].Value.ToString();                 cobItem.Tag = elemList[i].Attributes["areacode"].Value.ToString();                 pcobCountry.Items.Add(cobItem);             }         }

        public void GetProvince(ComboBox pcobCountry, ComboBox pcobProvince)         {             ComboBoxItem cobItem = (ComboBoxItem)pcobCountry.SelectedItem;             string selectCountry = cobItem.Content.ToString();             XmlDocument xmlDoc = new XmlDocument();             xmlDoc.Load(this._ConfigPath);             XmlNodeList elemList = xmlDoc.SelectSingleNode("AreaInfo").ChildNodes;             foreach (XmlNode node in elemList)             {                 string nodeValue = node.Attributes["name"].Value;                 if (nodeValue == selectCountry)                 {                     pcobProvince.Items.Clear();                     foreach (XmlNode node1 in node.SelectNodes("province"))                     {                         ComboBoxItem cobi = new ComboBoxItem();                         cobi.Content = node1.Attributes["name"].Value.ToString();                         cobi.Tag = node1.Attributes["provincecode"].Value.ToString();                         pcobProvince.Items.Add(cobi);                     }                     break;                 }             }         }

        public void GetCity(ComboBox pcobPronvice, ComboBox pcobCity)         {             if (pcobPronvice.SelectedItem!= null)             {                 ComboBoxItem cobItem = (ComboBoxItem)pcobPronvice.SelectedItem;                 string selectProvince = cobItem.Content.ToString();                 XmlDocument xmlDoc = new XmlDocument();                 xmlDoc.Load(this._ConfigPath);                 XmlNodeList elemlist = xmlDoc.SelectSingleNode("AreaInfo").ChildNodes;                 foreach (XmlNode node in elemlist)                 {                     foreach (XmlNode node1 in node.SelectNodes("province"))                     {                         string nodeValue = node1.Attributes["name"].Value.ToString();

                        if (nodeValue == selectProvince)                         {                             pcobCity.Items.Clear();                             foreach (XmlNode node2 in node1.SelectNodes("city"))                             {                                 ComboBoxItem cobi = new ComboBoxItem();                                 cobi.Content = node2.Attributes["name"].Value.ToString();                                 pcobCity.Items.Add(cobi);                             }                             break;                         }                     }                 }             }         }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值