Link to home
Start Free TrialLog in
Avatar of troyhalsey
troyhalsey

asked on

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>
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
1) Open an XML document
2) Load the file
3) Loop through each <FirstName> node
4) Add the text to the ListBox.

Bob
Avatar of troyhalsey
troyhalsey

ASKER

Bob...can you be a bit more clear.  Not sure where to put all of your code.
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
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.  
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
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"
doc.Load(Application.StartupPath & "\AddressBook.xml")

Change the name to your ListBox name:

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

Bob
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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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
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
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