We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Listbox populated by XML file

troyhalsey
troyhalsey asked
on
Medium Priority
432 Views
Last Modified: 2010-08-05
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>
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

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

Bob

Author

Commented:
Bob...can you be a bit more clear.  Not sure where to put all of your code.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

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.  
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

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"
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
doc.Load(Application.StartupPath & "\AddressBook.xml")

Change the name to your ListBox name:

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

Bob

Author

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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
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

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

Ask the Experts

Author

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?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

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

Author

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