简体   繁体   中英

C# Multiple bound listbox using XML file/

I have an XML file in the following format:

<World>
 <Country 1>
  <City 1>
    District 1
    District 2
    District 3
  </City 1>
  <City 2>
    District 1
    District 2
    District 3
  </City 2>
 </Country 1>
 <Country 2>
 ...
 </Country n>
</World>

I need to have 4 listbox such that the first one will contain the countries name. The second the city and the third the districts. Also, Whenever I chose a parent category all the subcategories related to the parent should be selected. That is to say, Selecting Country one should show and select cities of country one (but not the districts). Also I need the listboxes to update. Selecting both country 1 and 2 should show all the cities in country 1 and 2.

This is the code for what you requested (you need first to put 3 ListBox items on the page and wire their SelectedValueChange events):

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

namespace MultipleBoundListBox
{
    public partial class Form1 : Form
    {
        private static XmlDocument xmlDoc;
        private static XPathNavigator nav;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load("target.xml");

            nav = xmlDoc.DocumentElement.CreateNavigator();

            nav.MoveToFirstChild();
            var countries = new List<string>();
            countries.Add(nav.LocalName);

            while (nav.MoveToNext())
            {
                countries.Add(nav.LocalName);
            }

            listBox1.DataSource = countries;

            BindCities(countries[0]);
        }

        protected void BindCities(string country)
        {
            nav.MoveToRoot();
            var xpath = "/World/" + country;
            var countryElement = nav.SelectSingleNode(xpath);
            countryElement.MoveToFirstChild();

            var cities = new List<string>();
            cities.Add(countryElement.LocalName);

            while (countryElement.MoveToNext())
            {
                cities.Add(countryElement.LocalName);
            }

            listBox2.DataSource = cities;

            BindDistricts(country, cities[0]);
        }

        protected void BindDistricts(string country, string city)
        {
            nav.MoveToRoot();
            var xpath = "/World/" + country + "/" + city;
            var districtElement = nav.SelectSingleNode(xpath);
            districtElement.MoveToFirstChild();

            var districts = new List<string>();
            districts.Add(districtElement.LocalName);

            while (districtElement.MoveToNext())
            {
                districts.Add(districtElement.LocalName);
            }

            listBox3.DataSource = districts;
        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            BindCities((string)listBox1.SelectedValue);
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDistricts((string)listBox1.SelectedValue, (string)listBox2.SelectedValue);
        }
    }
}

XML needs to be in this form:

<World>
  <Nkvavn>
    <Rcltwkb>
      <Pjwrgsik />
      <Nemscmll />
      <Fnauarnbvw />
      <Egqpcerhjgq />
      <Olyhryyxi />
      <Vvlhtiee />
      <Wlsfhmv />
    </Rcltwkb>
    <Xudbhnakjb>
      <Cwxjtkteuji />
      <Fbtcvf />
      <Uviaceinhl />
    </Xudbhnakjb>
    <Kgujcymilwr>
      <Nlbvgtwoejo />
      <Tvufkvmryybh />
      <Xtomstcenmp />
      <Mhnngf />
      <Fjidqdbafxun />
    </Kgujcymilwr>
    <Taiyiclo>
      <Fiecxoxeste />
      <Loqxjq />
      <Vfsxfilxofe />
      <Hroctladlht />
    </Taiyiclo>
  </Nkvavn>
  <Tfrosh>
    <Tuqomkytlp>
      <Oyvivlvminhn />
      <Qeypvfgul />
      <Mbapjl />
    </Tuqomkytlp>
    <Rvxumtj>
      <Gkvigncdvgy />
      <Okcddyi />
      <Vvmacul />
    </Rvxumtj>
    <Pdjpgexuyc>
      <Yvsdmbckurju />
      <Bvkxvg />
      <Clmrvjwk />
      <Hdafjhydj />
      <Asauxtnoe />
      <Mwcviwmi />
    </Pdjpgexuyc>
  </Tfrosh>
</World>

Feel free to ask if you have any questions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM