我在这里查看了几个示例,看起来我正在遵循正确的程序但它仍然无法正常工作,所以我显然做错了。我有两个组合框,我试图从 XML 文件中填充这个想法是,一旦做出第一个选择,就会生成第二个组合框的一组选择,如果我切换第一个选择,那么第二个组合框应该刷新为好
这是我的 XML
<?xml version="1.0" encoding="utf-8"?>
<ComboBox>
<Customer name="John"/>
<Data>
<System>Linux</System>
</Data>
<Customer name="Fernando"/>
<Data>
<System>Microsoft</System>
<System>Mac</System>
</Data>
</ComboBox>
这是我的 C# 代码
//This part works the customer_comboBox is generated correctly with John and Fernando
XmlDocument doc = new XmlDocument();
doc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +@"\comboBox.xml");
XmlNodeList customerList = doc.SelectNodes("ComboBox/Customer");
foreach (XmlNode node in customerList)
{
customer_comboBox.Items.Add(node.Attributes["name"].InnerText);
}
//putting the selected item from the first comboBox in a string
string selected_customer = customer_comboBox.SelectedItem.ToString();
//This is the part that does not work
//I want to read XML node that matches selected_customer and populate systems available
foreach (XmlNode node in customerList)
{
if (node.Attributes["name"].InnerText == selected_customer)
{
foreach (XmlNode child in node.SelectNodes("ComboBox/Customer/Data"))
{
system_comboBox.Items.Clear();
system_comboBox.Items.Add(node["System"].InnerText);
}
}
}
我连续两天试图弄清楚这一点。不确定我的 XML 是否错误或我调用子节点的方式。