【发布时间】:2013-05-24 08:46:00
【问题描述】:
我正在尝试阅读我想为我妈妈制作的xml 文件。所以基本上这就是我想做的:
-
ComboBox将显示 XML 中的所有蔬菜名称。 - 选择蔬菜后,第二个
ComboBox将在 XML 中显示食谱名称,可以使用在第一个ComboBox中选择的蔬菜进行烹饪。 - 最后,使用 OK
Button,所选配方将读取指向该配方的文件路径。
我写的 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#代码
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("Recipe_List.xml");
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 阅读食谱?
【问题讨论】:
-
您应该将“potato”和“carrot”值放入它们自己的节点中。您只需要改进您的选择过程,您正在抓取一个高级节点并打印出其中的所有内容,而不是使用更具体的选择器来查找特定项目。
-
作为添加新节点的替代方法,您可以将蔬菜名称作为蔬菜节点的属性放入。