Steps to do:
1) Create class:
Public Class Customer
Private sName As String = String.Empty
Private sAddress1 As String = String.Empty
Private sAddress2 As String = String.Empty
Private sPhone As String = String.Empty
Private sFax As String = String.Empty
Private sCell As String = String.Empty
Private sOther As String = String.Empty
Private sAttn As String = String.Empty
Private sEmail As String = String.Empty
Private sSalesPerson As String = String.Empty
Public Property CustName As String
Get
Return sName
End Get
Set(value As String)
sName = value
End Set
End Property
Public Property Address1 As String
Get
Return sAddress1
End Get
Set(value As String)
sAddress1 = value
End Set
End Property
Public Property Address2 As String
Get
Return sAddress2
End Get
Set(value As String)
sAddress2 = value
End Set
End Property
Public Property Phone As String
Get
Return sAddress2
End Get
Set(value As String)
sAddress2 = value
End Set
End Property
Public Property Fax As String
Get
Return sFax
End Get
Set(value As String)
sFax = value
End Set
End Property
Public Property Cell As String
Get
Return sCell
End Get
Set(value As String)
sCell = value
End Set
End Property
Public Property Other As String
Get
Return sOther
End Get
Set(value As String)
sOther = value
End Set
End Property
Public Property Attn As String
Get
Return sAttn
End Get
Set(value As String)
sAttn = value
End Set
End Property
Public Property Email As String
Get
Return sEmail
End Get
Set(value As String)
sEmail = value
End Set
End Property
Public Property SalesPerson As String
Get
Return sSalesPerson
End Get
Set(value As String)
sSalesPerson = value
End Set
End Property
End Class
This class has the same members as your xml structure.
2) Create new Interface to be able to use class properties
Public Interface ICustomer
Property Name As String
Property Address1 As String
Property Address2 As String
Property Phone As String
Property Fax As String
Property Cell As String
Property Other As String
Property Attn As String
Property Email As String
Property SalesPerson As String
End
Read more about
Interfaces[
^]
3) Add new module. Define global variable
Customers
which holds the list of
Customer
. Create procedure/subroutine to load data from xml into
Customers
list.
Module ModMain
Public Customers As List(Of Customer) = Nothing
Sub LoadXmlData(sFileName As String)
Dim xDoc As XDocument = Nothing
Dim custList As IEnumerable = Nothing
Try
Customers = New List(Of Customer)
xDoc = XDocument.Load(sFileName, LoadOptions.PreserveWhitespace)
custList = From customer In xdoc.Elements("CustDB").Elements("Customer") _
Order By customer.Value Ascending _
Select Nm = customer.Elements("Name").Value, Ad1 = customer.Elements("Address1").Value, Ad2 = customer.Elements("Address2").Value _
, Ph = customer.Elements("Phone").Value, Fx = customer.Elements("Fax").Value, Ce = customer.Elements("Cell").Value _
, Oth = customer.Elements("Other").Value, Atn = customer.Elements("Attn").Value, Eml = customer.Elements("Email").Value _
, SaPe = customer.Elements("SalesPerson").Value
For Each c In custList
Dim cu As Customer = New Customer
With cu
.CustName = c.Nm
.Address1 = c.Ad1
.Address2 = c.Ad2
.Phone = c.Ph
.Fax = c.Fx
.Cell = c.Ce
.Other = c.Oth
.Attn = c.Atn
.Email = c.Eml
.SalesPerson = c.SaPe
End With
Customers.Add(cu)
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
Finally
xDoc = Nothing
End Try
End Sub
End Module
See:
IEnumerable Interface[
^]
4) follow this link to find out how to create binding context.
Simple Data Binding in WPF[
^] - step 2
<TextBox Text="{Binding CustName}" Margin="10" Grid.Column="1"></TextBox>
More at CP KB:
A Very Simple Example of Data Binding in WPF[
^]
WPF Data Binding - Part 1[
^]
5) Finally, use this code:
Class MainWindow
Private Sub CustCombo_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
Try
Dim cmb As ComboBox = sender
Dim i As Integer = cmb.SelectedIndex
Dim cust As Customer = Customers(i)
Me.DataContext = cust
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
End Try
End Sub
Public Sub New()
InitializeComponent()
Dim sFileName As String = "D:\custdbTesting.xml"
LoadXmlData(sFileName)
For Each c As Customer In Customers
Me.CustCombo.Items.Add(c.CustName)
Next
End Sub
End Class
Note: This is very basic sample. There's a lot to improve, for example: you can load data from xml to class via
xml serialization[
^].
A Complete Sample of Custom Class Collection Serialization and Deserialization[
^]
Here you can
download[^] a complete sample (without xml).
And the most important: i strongly recommend to read
MSDN documentation[
^].