Getting XML data into combobox

前端 未结 2 1190
情深已故
情深已故 2021-01-24 08:12

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



    
        

        
相关标签:
2条回答
  • 2021-01-24 08:22

    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...
    
    0 讨论(0)
  • 2021-01-24 08:45

    I prefer to use Linq2XML:

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

    var xmlDocument = XDocument.Load(fileName);
    

    or load from a string

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

    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();
    
    0 讨论(0)
提交回复
热议问题