1

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

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

2 回答 2

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();
于 2012-07-02T15:15:49.547 回答
2

使用 LINQ 到 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...
于 2012-07-02T15:19:15.807 回答