We help IT Professionals succeed at work.

Label based on Listbox drawn from XML file.

troyhalsey
troyhalsey asked
on
Medium Priority
239 Views
Last Modified: 2010-04-23
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
Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Awesome...thanks Carl! Got it working right away!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.