【发布时间】:2015-09-18 17:34:35
【问题描述】:
我正在尝试读取一个xml 文件并且我想这样做:
-
ComboBox将显示xml 中的所有蔬菜名称。 - 选择蔬菜后,第二个
ComboBox将显示xml 中的食谱名称,可以使用第一个ComboBox中选择的蔬菜进行烹饪。 - 最后,使用确定按钮,所选配方将读取指向该配方的文件路径。
我写的 XML
<Vegetables>
<vegetable name="Carrot">
<recipe name="ABCrecipe">
<FilePath>C:\\</FilePath>
</recipe>
<recipe name="DEFrecipe">
<FilePath>D:\\</FilePath>
</recipe>
</vegetable>
<vegetable name="Potato">
<recipe name="CBArecipe">
<FilePath>E:\\</FilePath>
</recipe>
<recipe name"FEDrecipe">
<FilePath>F:\\</FilePath>
</recipe>
</vegetable>
</Vegetables>
C#代码
public Form1()
{
InitializeComponent();
xDoc.Load("Recipe_List.xml");
}
XmlDocument xDoc = new XmlDocument();
private void Form1_Load(object sender, EventArgs e)
{
XmlNodeList vegetables = xDoc.GetElementsByTagName("Vegetable");
for (int i = 0; i < vegetables.Count; i++)
{
comboBox1.Items.Add(vegetables[i].Attributes["name"].InnerText);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//I'm lost at this place.
}
第一个ComboBox现在可以显示蔬菜名称,但是如何让第二个ComboBox根据xml文件读取食谱?
【问题讨论】:
标签: xml xml xml xml c# xml winforms