Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
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
 
Old April 6th, 2010, 04:35 PM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Unhappy 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!
 
Old April 7th, 2010, 03:34 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old April 7th, 2010, 08:15 AM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Question 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.
 
Old April 7th, 2010, 08:23 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
 
Old April 7th, 2010, 08:25 AM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Default

Oh, I'm sorry ... It's a Windows Forms ComboBox.
Don't think I even know what the other kind are.
 
Old April 7th, 2010, 08:36 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
QuadFather (April 7th, 2010)
 
Old April 7th, 2010, 08:40 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
QuadFather (April 7th, 2010)
 
Old April 7th, 2010, 09:01 AM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Question 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!
 
Old April 7th, 2010, 09:07 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
QuadFather (April 7th, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate combobox from ADO recordset - RESOLVED! robzyc Access VBA 8 May 23rd, 2008 01:07 AM
Populate ListView From ComboBox Select eusanpe C# 4 August 20th, 2007 06:49 PM
how to populate a table with xml data in xslt miccipynewbie XSLT 6 May 28th, 2007 07:29 AM
populate combobox in xsl mausumee XSLT 2 February 23rd, 2007 06:37 PM
populate data in a windows form combobox from a da anibiswas General .NET 0 March 4th, 2005 03:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.