這是你的要求是什麼代碼(你需要首先把頁面上的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>
隨意問,如果您有任何疑問
你需要(web表單,的WinForms等)什麼樣的應用? –
@Ravzan。我實際上需要一個Windows窗體(桌面應用程序)。幫助會大大降低。 – Drmanifold
我會嘗試實現這一點,並會發佈一個答案,如果我可以及時做到這一點,這是一個很好的做法:D –