c#How to populate a winform drop down box with XML data

ieg
ieg used Ask the Experts™
on
I would like to populate a drop down box (list or combo) with multiple columns of data from an XML file. Could someone advise on the best technique please.
The XML looks like this
<root>
<Form Name='F1' Description='Test Form 1'/>
<Form Name='F2' Description='Test Form 2'/>
</root>
I have loaded the XML into a DOM and have created a nodelist of the Form Elements which I can iterate over. The bit I can't get my head around is the commands for adding the data.
Thanks

Andy
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi,

Is it this you're looking for?

                  XmlDocument doc = new XmlDocument();
                  doc.LoadXml(@"<root>
<Form Name='F1' Description='Test Form 1'/>
<Form Name='F2' Description='Test Form 2'/>
</root>
");
                  foreach (XmlNode node in doc.DocumentElement.ChildNodes) {
                        comboBox1.Items.Add(node.Attributes["Name"].Value);
                  }


/peter
ieg

Author

Commented:
Hi Peter,
Thanks, this looks good - how would I add a second column?

Andy
Commented:
Ah, I missed that requirement. AFAIK you can't do that with the standard framework. But there are some free controls out there which could accomplish that. Have, for example, a look at

http://www.codeproject.com/KB/architecture/MultiColumnFlatCombo.aspx
and
http://www.codeproject.com/KB/custom-controls/Multi_Column_Combo_Box.aspx
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

anarki_jimbelSenior Developer

Commented:
You may also use data binding approach. It better suites for multicolumn controls (like grids).
However it's not possible for a standard combo.

Still, have a look at the snippet. If you go with dataviewgrid you don't need the last line.
        private void button3_Click(object sender, EventArgs e)
        {

            DataSet ds = new DataSet();
            ds.ReadXml("XMLFile1.xml");
            DataTable dt = ds.Tables[0];
            this.comboBox1.DataSource = dt;
            this.comboBox1.DisplayMember = "Name";
          }

Open in new window

ieg

Author

Commented:
Many thanks for your replies. I just find it odd that at the very least a combo box would require 2 columns, a key field and a description. Even in MS Access this is a trivial task.
I fear that I will have to adopt a third party control.

Commented:
You could have the description (for each item) in the tooltip. But that would also require a 3rd party control or your own extension to the standard control.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial