 |
| XML General XML discussions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 6th, 2010, 04:35 PM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
How to populate combobox with xml using VB?
I'm using VB (Visual Studio 2008).
I want to populate ComboBox1 with the items listed in Profiles.xml
Here is the xml file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<Profiles>
<Name>
</Name>
</Profiles>
I just want the combo box to list all the names ("Name") in the xml file ("Profiles.xml"). Can't find any help online, for the life of me.
Please help! 
|
|

April 7th, 2010, 03:34 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well I searched for "ComboBox populate XML" and the very first link was to a code sample on how to do it.
Admittedly that was in C#, so let us know if its too hard to translate: http://www.debugging.com/bug/9439
|
|

April 7th, 2010, 08:15 AM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Already tried that ...
Yeah, I found a whole bunch of things saying how to do it in C, but nothing on how to do it in VB. I tried modifying the code for VB, but I'm afraid I'm just not that good yet.
I think I need a little more help. 
|
|

April 7th, 2010, 08:23 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
You haven't told us what kind of combo box that is (e.g. Windows Forms, ASP.NET, WPF). As for the XML part with VB.NET you can use LINQ to XML: LINQ to XML
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

April 7th, 2010, 08:25 AM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Oh, I'm sorry ... It's a Windows Forms ComboBox.
Don't think I even know what the other kind are. 
|
|

April 7th, 2010, 08:36 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well a small bit of code I wrote looks like this:
Code:
Dim doc As XDocument = XDocument.Load("Profiles.xml")
ComboBox1.DataSource = (From element In doc.Descendants("Name") Select element.Value).ToList()
but there may be too much in there to understand if you are just beginning.
A similar bit of code, which is closer to the one I linked to is as follows:
Code:
Dim doc As XDocument = XDocument.Load("Profiles.xml")
For Each element As XElement In doc.Descendants("Name")
ComboBox1.Items.Add(element.Value)
Next
Or if you want the old school version, which is a completed conversion of what I linked to:
Code:
Dim doc As New XmlDocument()
doc.Load("Profiles.xml")
Dim nameList As XmlNodeList = doc.SelectNodes("/Profiles/Name")
For Each name As XmlNode In nameList
ComboBox1.Items.Add(name.InnerText)
Next
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

April 7th, 2010, 08:40 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Pseudo code that first clears the ComboBox.Items collection and then adds the strings contained in the 'Name' elements is as follows:
Code:
comboBox1.Items.Clear()
comboBox1.Items.AddRange(XElement.Load("Profiles.xml").<Name>.Select(Function(n) n.Value).ToArray())
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

April 7th, 2010, 09:01 AM
|
|
Registered User
|
|
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
|
|
Fantastic ... one last little question ...
Fantastic!
Now, it would be perfect if I could alphabetize the items in the combobox ... any easy ways to do that?
Just so everyone know, I really am looking for this stuff on my own, but I'm find it very difficult to find exactly the info I need. Most of it seems really generic, and I'd have to modify it ... I'm just a newbie! 
|
|

April 7th, 2010, 09:07 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
If you want to order the items alphabetically then the following change does that:
Code:
ComboBox1.Items.AddRange(XElement.Load(Application.StartupPath + "\Profiles.xml").<Name>.Select(Function(n) n.Value).OrderBy(Function(s) s).ToArray())
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|
 |