?
Solved

Listbox populated by XML file

Posted on 2006-04-05
15
Medium Priority
?
394 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>
0
Comment
Question by:troyhalsey
  • 7
  • 7
15 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16384829
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
 
LVL 96

Expert Comment

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

Bob
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:troyhalsey
ID: 16385015
Bob...can you be a bit more clear.  Not sure where to put all of your code.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16385044
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
 

Author Comment

by:troyhalsey
ID: 16385080
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16385123
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
 

Author Comment

by:troyhalsey
ID: 16385270
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16385300
doc.Load(Application.StartupPath & "\AddressBook.xml")

Change the name to your ListBox name:

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

Bob
0
 

Author Comment

by:troyhalsey
ID: 16385578
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 16385600
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
 

Author Comment

by:troyhalsey
ID: 16385631
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16385793
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
 

Author Comment

by:troyhalsey
ID: 16385834
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
 

Author Comment

by:troyhalsey
ID: 16385909
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question