1

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>

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";  
JackyBoi
  • 2,695
  • 12
  • 42
  • 74

2 Answers2

4

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();
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • This is such a great answer, but one thing i dont understand is that why is that when i do this var xmlDocument = XDocument.Parse(@"C:\\Users\\jackandjill\\Documents\\Visual Studio 2010\\Projects\\highlite\\highlite\\keys.xml"); i get an error saying "Data at the root level is invalid. Line 1, position 1." – JackyBoi Jul 02 '12 at 22:09
  • To load from a file you use `XDocument.Load` instead. Besides, don't use both `@` and double backslash (`\\`). `@` turns off all escaping. – Albin Sunnanbo Jul 03 '12 at 05:11
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...
SNH
  • 115
  • 7
  • Tks so much for showing me another reason to learn LINQ! but one thing i dont understand is that why is that when i do this var xmlDocument = XDocument.Parse(@"C:\\Users\\jackandjill\\Documents\\Visual Studio 2010\\Projects\\highlite\\highlite\\keys.xml"); i get an error saying "Data at the root level is invalid. Line 1, position 1." – JackyBoi Jul 02 '12 at 22:11
  • @JackyBoi - There is nothing wrong with your xml, but `XDocument.Parse` is for loading xml from a string, not a file name, so try doing `XDocument.Load` instead. – SNH Jul 03 '12 at 01:49
  • haha funny enough on my way to work this struck me aswell and i tried it and works fine..tks so much! – JackyBoi Jul 03 '12 at 03:51