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</PostalC ode>
<Country>USA</Country>
<Email>thalsey@amspg.com</ Email>
</Address>
- <Address>
<FirstName>Brenda</FirstNa me>
<LastName>Howard</LastName >
<CompanyName>AMS Production Group</CompanyName>
<Address1>2000 Custer</Address1>
<City>Richardson</City>
<Region>Texas</Region>
<PostalCode />
<Country />
<Email />
</Address>
</Addresses>
</AddressBook>
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</PostalC
<Country>USA</Country>
<Email>thalsey@amspg.com</
</Address>
- <Address>
<FirstName>Brenda</FirstNa
<LastName>Howard</LastName
<CompanyName>AMS Production Group</CompanyName>
<Address1>2000 Custer</Address1>
<City>Richardson</City>
<Region>Texas</Region>
<PostalCode />
<Country />
<Email />
</Address>
</Addresses>
</AddressBook>
1) Open an XML document
2) Load the file
3) Loop through each <FirstName> node
4) Add the text to the ListBox.
Bob
2) Load the file
3) Loop through each <FirstName> node
4) Add the text to the ListBox.
Bob
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.Debugg erStepThro ugh()> 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 designerPlaceholderDeclara tion 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("b in\test.xm l"))
For Each node As XmlNode In doc.SelectNodes("//FirstNa me")
Me.ListBox1.Items.Add(node .InnerText )
Next
End Sub
End Class
Bob
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.Debugg
End Sub
Protected WithEvents ListBox1 As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
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("b
For Each node As XmlNode In doc.SelectNodes("//FirstNa
Me.ListBox1.Items.Add(node
Next
End Sub
End Class
Bob
ASKER
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.ICon tainer
'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.ListB ox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.listNames = New System.Windows.Forms.ListB ox
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.listNam es)
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.Start upPath & "\settings.xml")
For Each node As XmlNode In doc.SelectNodes("//FirstNa me")
Me.listNames.Items.Add(nod e.InnerTex t)
Next
End Sub
End Class
Bob
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.ICon
'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.ListB
<System.Diagnostics.Debugg
Me.listNames = New System.Windows.Forms.ListB
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.listNam
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.Start
For Each node As XmlNode In doc.SelectNodes("//FirstNa
Me.listNames.Items.Add(nod
Next
End Sub
End Class
Bob
ASKER
Bob, this code isn't working it is getting hung up on...
doc.Load(Application.Start upPath & "\settings.xml")<----is setting supposed to be my file? my file is called "Addressbook.XML"
And...
Me.listNames.Items.Add(nod e.InnerTex t) saying it doesn't recognize "listNames"
doc.Load(Application.Start
And...
Me.listNames.Items.Add(nod
doc.Load(Application.Start upPath & "\AddressBook.xml")
Change the name to your ListBox name:
Me.ListBox1.Items.Add(node .InnerText )
Bob
Change the name to your ListBox name:
Me.ListBox1.Items.Add(node
Bob
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
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
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
ASKER
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
Troy
...
Dim doc As New XmlDocument
doc.Load("c:\temp\test.xml
For Each node As XmlNode In doc.SelectNodes("//FirstNa
Me.ListBox1.Items.Add(node
Next
Bob