VB.net populate combobox from xml file

i have a vb.net windows forms application.  I want to read in an xml file and populate a combo box
Here is the an example of what will be in the xml file
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Main>
  <SiteInfo>
    <Element Name="NewYork" Value="9.9.9.9" />
    <Element Name="LA" Value="2.2.2.2" />
    <Element Name="Boston" Value="3.3.3.3" />
  </SiteInfo>
</Main>

Open in new window


I want to read in the information from the xml file and have the Name be the text entry (what is displayed to the user) for a combo box and the value be the value of the combo box... for the life of me I cant figure it out

the comobox name = cmbSiteInfo

Thanks so much for the help
David ModugnoAsked:
Who is Participating?
 
it_saigeConnect With a Mentor DeveloperCommented:
Like this:

Form1.vb -
Public Class Form1
	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		Dim xelement As XElement = XElement.Load("Locations.xml")
		Dim locations As IEnumerable(Of XElement) = xelement.Descendants("SiteInfo").Descendants("Element")
		ComboBox1.DataSource = (From element In locations Select New With {.Display = element.Attribute("Name").Value, .Value = element.Attribute("Value").Value}).ToList()
		ComboBox1.DisplayMember = "Display"
		ComboBox1.ValueMember = "Value"
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
	Inherits System.Windows.Forms.Form

	'Form overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	<System.Diagnostics.DebuggerStepThrough()> _
	Private Sub InitializeComponent()
		Me.ComboBox1 = New System.Windows.Forms.ComboBox()
		Me.SuspendLayout()
		'
		'ComboBox1
		'
		Me.ComboBox1.FormattingEnabled = True
		Me.ComboBox1.Location = New System.Drawing.Point(13, 13)
		Me.ComboBox1.Name = "ComboBox1"
		Me.ComboBox1.Size = New System.Drawing.Size(259, 21)
		Me.ComboBox1.TabIndex = 0
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 59)
		Me.Controls.Add(Me.ComboBox1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub

	Friend WithEvents ComboBox1 As ComboBox
End Class

Open in new window

Which produces the following output -Capture.PNG
-saige-
0
 
David ModugnoAuthor Commented:
Thank you... i will give that a try
0
 
David ModugnoAuthor Commented:
Worked perfectly.. thank you
0
 
it_saigeDeveloperCommented:
Glad to be of assistance...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.