将 XML 数据放入组合框 [英] Getting XML data into combobox

查看:20
本文介绍了将 XML 数据放入组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个组合框和一个 XML 文件的 Windows 窗体,如下所示

I have a windows form with 3 comboboxes and a XML file as following

<?xml version="1.0"?>
<shrtcutkeys>
    <Keysmain>
        <keychars>
            <key1>
                Ctrl
            </key1>
            <key1>
                Alt
            </key1>
            <key1>
                Shift
            </key1>
        </keychars>
    </Keysmain>
    <Seckeys>
        <keychars>
            <key2>
                Ctrl
            </key2>
            <key2>
                Alt
            </key2>
            <key2>
                Shift
            </key2>
        </keychars>
    </Seckeys>
    <Alphas>
        <keychars>
            <key3>
                a
            </key3>
            <key3>
                b
            </key3>
            <key3>
                c
            </key3>
        </keychars>
    </Alphas>
</shrtcutkeys>

所以我想在combobox1中显示所有key1,在combox2中显示所有key2等等......尝试这样做,但没有真正起作用

So i would like to display all key1 in combobox1 and all key2 in combox2 and so on so forth..tried doing this but not really working

DataSet dsSet = new DataSet();
            dsSet.ReadXml("C:\Users\jackandjill\Documents\Visual Studio 2010\Projects\highlite\highlite\keys.xml");
            comboBox1.DataSource = dsSet.Tables["keys"];
            comboBox1.DisplayMember = "key1";  

推荐答案

我更喜欢使用 Linq2XML:

I prefer to use Linq2XML:

将数据加载到 XDocument:
从文件加载:

Load the data into an XDocument:
Either load from file:

var xmlDocument = XDocument.Load(fileName);

或从字符串加载

var xmlDocument = XDocument.Parse( @"<?xml version=""1.0""?>
<shrtcutkeys>
    <Keysmain>
        <keychars>
            <key1>
                Ctrl
            </key1>
            <key1>
                Alt
            </key1>
            <key1>
                Shift
            </key1>
        </keychars>
    </Keysmain>
    <Seckeys>
        <keychars>
            <key2>
                Ctrl
            </key2>
            <key2>
                Alt
            </key2>
            <key2>
                Shift
            </key2>
        </keychars>
    </Seckeys>
    <Alphas>
        <keychars>
            <key3>
                a
            </key3>
            <key3>
                b
            </key3>
            <key3>
                c
            </key3>
        </keychars>
    </Alphas>
</shrtcutkeys>");

然后您可以选择所需的项目

Then you can select desired items

var mainItems = from key in xmlDocument.Descendants("key1")
                select key.Value;
var secKeyItems = from key in xmlDocument.Descendants("key2")
                select key.Value;
var alphaItems = from key in xmlDocument.Descendants("key3")
                select key.Value;

您现在可以将每个组合绑定到所选结果,如下所示:

You can now bind each combo to the selected result, like this:

comboBox1.DataSource = mainItems.ToList();

您可能想要清洗您的 XML(以删除换行符和空格)

You might want to wash your XML (to remove newlines and spaces)

var mainItems = from key in xmlDocument.Descendants("key1")
                select key.Value.Trim();
var secKeyItems = from key in xmlDocument.Descendants("key2")
                select key.Value.Trim();
var alphaItems = from key in xmlDocument.Descendants("key3")
                select key.Value.Trim();

这篇关于将 XML 数据放入组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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