且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 XML 文档中读取值以构建 ComboBox?

更新时间:2023-11-15 12:09:10

你的 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?​​a>

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