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

来源:互联网 发布:广州橙子网络做什么的 编辑:程序博客网 时间:2024/05/01 10:34

  #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;                        }                    }                }            }        }