Listbox populated by XML file

I am new to VB.net so I need lots of explination...sorry in advance.

Below is my xml file...it is located in the project's BIN folder.  How do I access it and populate listbox1 with just the first names?  Ultimately, I want to select the name and beside the list box various labels will display the rest of this information for the specific person.  Anyone that can explain this in layman's terms will be duly praised.  Thanks!


 <?xml version="1.0" ?>
- <AddressBook xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <Addresses>
- <Address>
  <FirstName>Troy</FirstName>
  <LastName>Halsey</LastName>
  <CompanyName>AMS Production Group</CompanyName>
  <Address1>16986 N. Dallas Parkway</Address1>
  <City>Dallas</City>
  <Region>Texas</Region>
  <PostalCode>75248</PostalCode>
  <Country>USA</Country>
  <Email>thalsey@amspg.com</Email>
  </Address>
- <Address>
  <FirstName>Brenda</FirstName>
  <LastName>Howard</LastName>
  <CompanyName>AMS Production Group</CompanyName>
  <Address1>2000 Custer</Address1>
  <City>Richardson</City>
  <Region>Texas</Region>
  <PostalCode />
  <Country />
  <Email />
  </Address>
  </Addresses>
  </AddressBook>
troyhalseyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Imports System.Xml

...


Dim doc As New XmlDocument
doc.Load("c:\temp\test.xml")

For Each node As XmlNode In doc.SelectNodes("//FirstName")
  Me.ListBox1.Items.Add(node.InnerText)
Next

Bob
0
Bob LearnedCommented:
1) Open an XML document
2) Load the file
3) Loop through each <FirstName> node
4) Add the text to the ListBox.

Bob
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

troyhalseyAuthor Commented:
Bob...can you be a bit more clear.  Not sure where to put all of your code.
0
Bob LearnedCommented:
Troy,

Is this an ASP.NET application?  If so, then it's like this for ASP.NET 1.1:

Imports System.Xml

Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

      End Sub
      Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim doc As New XmlDocument
      doc.Load(Server.MapPath("bin\test.xml"))

       For Each node As XmlNode In doc.SelectNodes("//FirstName")
          Me.ListBox1.Items.Add(node.InnerText)
      Next
    End Sub

End Class

Bob
0
troyhalseyAuthor Commented:
No this is a windows application.  I do not want to use Asp.Net or Sql.  I want the xml file to be the only outside reference.  
0
Bob LearnedCommented:
For WinForms in 2003, it's like this:

Imports System.Xml

Public Class frmListBox
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  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.
  Friend WithEvents listNames As System.Windows.Forms.ListBox
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.listNames = New System.Windows.Forms.ListBox
    Me.SuspendLayout()
    '
    'listNames
    '
    Me.listNames.Location = New System.Drawing.Point(16, 28)
    Me.listNames.Name = "listNames"
    Me.listNames.Size = New System.Drawing.Size(120, 186)
    Me.listNames.TabIndex = 0
    '
    'frmListBox
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 266)
    Me.Controls.Add(Me.listNames)
    Me.Name = "frmListBox"
    Me.Text = "Load ListBox"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub frmListBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim doc As New XmlDocument
    doc.Load(Application.StartupPath & "\settings.xml")

    For Each node As XmlNode In doc.SelectNodes("//FirstName")
      Me.listNames.Items.Add(node.InnerText)
    Next

  End Sub

End Class

Bob
0
troyhalseyAuthor Commented:
Bob, this code isn't working it is getting hung up on...

doc.Load(Application.StartupPath & "\settings.xml")<----is setting supposed to be my file? my file is called "Addressbook.XML"
And...
Me.listNames.Items.Add(node.InnerText)  saying it doesn't recognize "listNames"
0
Bob LearnedCommented:
doc.Load(Application.StartupPath & "\AddressBook.xml")

Change the name to your ListBox name:

  Me.ListBox1.Items.Add(node.InnerText)

Bob
0
troyhalseyAuthor Commented:
Got it! Score one for Bob's short answers!

So....my next question, Bob, is how would I make a label next to the listbox display the correct region for the selected name?

Thanks

Troy
0
Bob LearnedCommented:
If your label is called Label1, then use this:

  Private Sub listNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listNames.SelectedIndexChanged
    Me.Label1.Text = Me.ListBox1.SelectedItem
  End Sub

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
troyhalseyAuthor Commented:
Right...got that.  My question was...how do I get it to display the correct Region ("Texas")....wouldn't it have to refer back to the xml file again to pull the info based on the name selected?
0
Bob LearnedCommented:
Troy,

I often get involved in these long-term questions, and right now I really don't have the time to devote to a continuously evolving question.

Bob
0
troyhalseyAuthor Commented:
Bob,

How am I going to explain my question from this point???  I was so close...if you aswer this one for me, I will close this thread.  Sorry to be a bother.

Troy
0
troyhalseyAuthor Commented:
Actually nevermind.  Don't offer to help in the future if you don't have time.  You were very helpful and gave me a great start, so I will close this thread with your points given.  But please do not belittle the users on this site because they are wanting to learn from you, I apologize for the ongoing questioning.  You seemed to be very skilled and I was eager to learn.  Thanks for your time.

Troy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.