Label based on Listbox drawn from XML file.

Alright, here is the scenario.  I will try and keep this question quick as I apparently can run a question on and on.

I have a list box that derives its items from an xml file.
I would like to have it work as such:

User clicks name in Listbox...and the label next to the listbox read information from another node in the xml file.

My XML file is as follows:

 <?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>

My current code is as follows:

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

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

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

Thank you in advance for your help.

Troy
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.

Carl TawnSystems and Integration DeveloperCommented:
OK, to do this you will probably need to keep your XML file loaded in memory. You can do this by moving the "Dim doc As New XmlDocument" declaration outside of the method.

You can then grab the Address node relating to the LastName selected from the listbox with something like:

    Dim doc As XmlDocument

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

        doc = New XmlDocument
        doc.Load(Application.StartupPath & "\AddressBook.xml")

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

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim node As XmlNode = doc.SelectSingleNode("//Address[LastName='" + ListBox1.SelectedItem + "']")
        Label1.Text = node.ChildNodes(0).InnerText

    End Sub
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:
Awesome...thanks Carl! Got it working right away!
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.