2011-08-04 22 views
0

我有以下格式的XML文件:使用XML文件綁定列表框C#多/

<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> 

我需要有4個列表框,使得第一個將包含國家名稱。第二個城市和第三個地區。此外,每當我選擇一個父類別時,應該選擇與父類相關的所有子類別。也就是說,選擇國家應顯示並選擇國家一(但不是地區)的城市。 另外我需要列表框來更新。選擇這兩個國家1和2應顯示在國家1中的所有城市和2

+0

你需要(web表單,的WinForms等)什麼樣的應用? –

+0

@Ravzan。我實際上需要一個Windows窗體(桌面應用程序)。幫助會大大降低。 – Drmanifold

+0

我會嘗試實現這一點,並會發佈一個答案,如果我可以及時做到這一點,這是一個很好的做法:D –

回答

0

這是你的要求是什麼代碼(你需要首先把頁面上的ListBox 3項和它們的連線事件SelectedValueChange):

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需要在這種形式:

<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> 

隨意問,如果您有任何疑問

+0

謝謝!我不再需要它,但是這段代碼肯定會證明是有用的。 – Drmanifold