将XML数据导入组合框

时间:2012-07-02 15:02:11

标签: c# xml combobox

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

<?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,依此类推。比如说这样做但是没有真正起作用

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

2 个答案:

答案 0 :(得分:4)

我更喜欢使用Linq2XML:

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

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

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

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;

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

comboBox1.DataSource = mainItems.ToList();

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

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

答案 1 :(得分:2)

Usinq LINQ To XML:

var xml = XElement.Parse(
                @"<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>");

var key1 = xml.Descendants("key1");

foreach (var key in key1)
    comboBox1.Items.Add(key.Value.Trim());

var key2 = xml.Descendants("key2");

foreach (var key in key2)
    comboBox2.Items.Add(key.Value.Trim());

//Do the same for other keys...