如何从 XML 文档中读取值以构建 ComboBox? [英] How do I read values from an XML document to build a ComboBox?

查看:39
本文介绍了如何从 XML 文档中读取值以构建 ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读 我想为我妈妈制作的文件.所以基本上这就是我想要做的:

I'm trying to read an xml file which I want to make for my mom. So basically this is what I want to do:

  1. 一个 ComboBox,它将显示 XML 中的所有蔬菜名称.
  2. 选择蔬菜后,第二个ComboBox 将在 XML 中显示食谱名称,可以使用第一个 ComboBox 中选择的蔬菜进行烹饪.
  3. 最后,点击 OK Button,所选配方将读取通向该配方的文件路径.
  1. A ComboBox which will show all the vegetable names in the XML.
  2. After selecting a vegetable, the second ComboBox will show the recipe names in the XML that could use the vegetable selected in the first ComboBox for cooking.
  3. Last, with a OK Button, the selected recipe will read the file path which leads to the recipe.

我写的 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 读取菜谱?

The first ComboBox is now able to display the vegetable names, but how do I make the 2nd ComboBox to read the recipes?

推荐答案

你的 xml 应该重新构造,因为你在把配方名称和文件路径节点作为配方节点的值时正在混合数据

Your xml should be restructured, as you are mixing data when putting recipe names and file path node as the value of the recipe node

这里有一个更好的方法:

Here is a better approach:

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

所以要显示菜谱,你需要提取菜谱节点的属性.此处解释了如何执行此操作:How to read attribute value fromC#中的XmlNode?

So to display the recipes you need to extract the attribute of the recipe node. How to do this is explained here: How to read attribute value from XmlNode in C#?

由于注释而更正了 xml 结构.谢谢

Corrected xml structure due to comments. Thanks

这篇关于如何从 XML 文档中读取值以构建 ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆