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

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

问题描述

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

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等等。 .tried do this but not really work

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();


$ b b

您可能想要清除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天全站免登陆