XML - Source DataConsideration: The data object will contain is base information and include multiples of addresses/phones/staff
<agency>
<name>Bob's Agency</name>
<account>12341234214</account>
<expirationDate>9/4/2011 12:00:00 AM</expirationDate>
<addresses>
<address>
<addressType>Mailing</addressType>
<address1>1234 Main Street</address1>
<address2></address2>
<city>New Orleans</city>
<state>LA</state>
<zip>70112</zip>
<county>New Orleans</county>
</address>
</addresses>
<phones>
<phone>
<phoneType>Home</phoneType>
<number>(800) xxx-xxxx</number>
</phone>
<phone>
<phoneType>Office</phoneType>
<number>(800) xxx-xxxx</number>
</phone>
</phones>
<staff>
<person>
<manager>0</manager>
<id>7549</id>
<fname>Peter</fname>
<lname>Gibbons</lname>
</person>
<person>
<manager>1</manager>
<id>D0Rk</id>
<fname>Bill</fname>
<lname>Lumbergh</lname>
</person>
</staff>
</agency>
Class ObjectConsideration: I decided to go with a class object that will be painted directly onto the html page. I wanted to keep it simple and I prefer to call objects directly. I use LINQ to pull the root nodes but the beauty of LINQ is the ability to include WHERE/SORT/DISTINCT.
Public Class Entity_Agency
Private _name As String
Private _account As String
Private _expirationdate As Date
Private _locations as String 'This is a bonus item to list all distinct locations on the DataTable
Private _addresses As List(Of Address)
Private _phones As List(Of Phone)
Private _staffing As List(Of Person)
Structure Address
Public Type As String
Public Address1 As String
Public Address2 As String
Public City As String
Public State As String
Public Zip As String
Public County As String
End Structure
Structure Phone
Public Type As String
Public Number As String
End Structure
Structure Person
Public Manager As Integer
Public StaffID As String
Public FirstName As String
Public LastName As String
End Person
ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
...........skipping a few properties.....
ReadOnly Property Addresses() As List(Of Address)
Get
Return _addresses
End Get
End Property
ReadOnly Property Phones() As List(Of Phone)
Get
Return _phones
End Get
End Property
ReadOnly Property Staffing() As List(Of Person)
Get
Return _staffing
End Get
End Property
Public Sub New(ByVal Name As String, ByVal Account As String, _
ByVal ExpirationDate As Date, ByVal Locations As String, _
ByVal Addresses As List(Of Address), _
ByVal Phones As List(Of Phone), _
ByVal Staffing As List(Of Person))
_name = Name
_account = Account
_expirationdate = ExpirationDate
_locations = Locations
_addresses = Addresses
_phones = Phones
_staffing = Staffing
End Sub
Public Function GetAgencies(ByVal xmlFile As String) As Collection
Dim myAgencies As New Collection
Dim myDoc As XDocument
myDoc = XDocument.Load(xmlFile)
Dim myQuery = From myXE In myDoc.Descendants.Elements("agency")
For Each myXE As XElement In myQuery
'Get all addresses associated to agency
Dim myAddressList As New List(Of Address) 'declare list container
Dim myLocations As String = "" 'collects distinct city/state combo
For Each xeAddressItem As XElement In myXE.<addresses>.<address> 'iterate through each xml address
Dim myAddressItem As Address
myAddressItem.Type = xeAddressItem.<addressType>.Value
myAddressItem.Address1 = xeAddressItem.<address1>.Value
myAddressItem.Address2 = xeAddressItem.<address2>.Value
myAddressItem.City = xeAddressItem.<city>.Value
myAddressItem.State = xeAddressItem.<state>.Value
Dim tempCityState As String = xeAddressItem.<city>.Value + ", " + xeAddressItem.<state>.Value
If (myLocations.Contains(tempCityState) = False) Then
If (myLocations = "") Then
myLocations = tempCityState
Else
myLocations += "<br/>" + tempCityState 'added <br/> to keep each city/state combo own line
End If
End If
myAddressItem.Zip = xeAddressItem.<zip>.Value
myAddressItem.County = xeAddressItem.<county>.Value
myAddressList.Add(myAddressItem)
Next
'Get all phonenumbers associated to agency
Dim myPhoneList As New List(Of Phone) 'declare list container
For Each xePhoneItem As XElement In myXE.<phones>.<phone> 'iterate through each xml address
Dim myPhoneItem As Phone
myPhoneItem.Type = xePhoneItem.<phoneType>.Value
myPhoneItem.Number = xePhoneItem.<number>.Value
myPhoneList.Add(myPhoneItem)
Next
'Get all agency staffing
Dim myStaffList As New List(Of Person) 'declare list container
Dim myPersonQuery= From personXE In myXE.<staff>.<person> Order By personXE .<manager>.Value Descending
For Each xePersonItem As XElement In myPersonQuery
Dim myPersonItem As Person
myPersonItem.Manager = xePersonItem.<manager>.Value
myPersonItem.StaffID = xePersonItem.<id>.Value
myPersonItem.FirstName = xePersonItem.<fname>.Value
myPersonItem.LastName = xePersonItem.<lname>.Value
myStaffList.Add(myPersonItem)
Next
'Put everything into my class object now
myAgencies.Add( _
New Entity_Agency( _
myXE.<name>.Value, _
myXE.<account>.Value, _
myXE.<expirationDate>.Value, _
myLocations, _
myAddressList, _
myPhoneList, _
myStaffList))
Next
Return myAgencies
End Function
End Class
Calling .Net Form (CODE BEHIND)Consideration: I'm passing my object to a List Control so I use the Contol's Databind method. If you wanted to declare an object in the code behind you can reference that directly on the .NET design by binding it to the page instead. I posted both Method 1/Method 2.
'METHOD 2 global form collection declare
'The below myAgencies declare needs to go above all page events/code but below Page declaration
'Public myAgencies As New Collection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
''Run if not a postback
If (Page.IsPostBack = False) Then
Dim myAgencies As New Entity_Agency
'METHOD 1:
'This code is used if you want to pass directly to a .NET ListView/Repeater control
lstAgency.DataSource = myAgencies.GetAgencies( <your xml file address here>)
lstAgency.DataBind()
'METHOD 2:
'Use the below code if you want to set an object you declared globally to the page and will call it in the html
'myAgencies = myAgencies.GetAgencies( <your xml file address here>)
'Page.DataBind()
End If
End Sub
Calling .Net Form (HTML)
<!-- This needs to be at the top of the page -->
<!-- By calling our project's namespace we can reference the class objects we created; using them as templates -->
<%@ Import Namespace="YOURNAMESPACE" %>
<!-- METHOD 1 : Binding to .NET Control -->
<!-- Populating our Listview -->
<!-- agency for ID is what I pass to DataTable -->
<table id="agency" cellpadding="0" cellspacing="0" border="0" class="display">
<thead>
<tr>
<th>Agency Name</th>
<th>Account Number</th>
<th>Expiration Date</th>
<th>Total Staffing</th>
<th>Locations</th>
<th>Phone Numbers</th> <!-- I use the detail option to show this column under the main row -->
<th>Addresses</th> <!-- I use the detail option to show this column under the main row -->
<th>Staffing</th> <!-- I use the detail option to show this column under the main row -->
</tr>
</thead>
<tbody>
<asp:ListView ID="lstAgency" runat="server">
<LayoutTemplate>
<asp:Literal ID="itemPlaceholder" runat="server"></asp:Literal>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Name %></td>
<td><%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Account %></td>
<td><%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).DateExpiration.ToString("MM/dd/yyyy")) %</td>
<td><%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Staffing.Count %></td>
<td><%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Locations %></td>
<td>
<!-- show sublist of phones -->
<asp:Repeater ID="rPhones" runat="server"
DataSource='<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Phones.ToList %>'>
<ItemTemplate>
<b><%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Phone).Type %></b>
<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Phone).Number %><br />
</ItemTemplate>
</asp:Repeater>
</td>
<td>
<asp:Repeater ID="rAddresses" runat="server"
DataSource='<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Addresses.ToList %>'>
<ItemTemplate>
<b><%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Address).Type %></b>
<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Address).Address1 %>
<%#If(DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Address).Address2.ToString() <> "", "", DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Address).Address2.ToString()) %>
<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Address).City %> ,
<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Address).State %>
<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Address).Zip %> <br />
</ItemTemplate>
</asp:Repeater>
</td>
<td>
<asp:Repeater ID="rInvestigators" runat="server"
DataSource='<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Staffing.ToList %>'>
<ItemTemplate>
<b><%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Person).StaffID %></b>:
<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Person).LastName %>
<%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Person).FirstName %>
<br />
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</tbody>
</table>
<!-- METHOD 2 : Response.Write -->
<!-- Calling data collection object we declared in code behind -->
<table id="agencysearch" cellpadding="0" cellspacing="0" border="0" class="display">
<thead>
<tr>
<th>Agency Name</th>
<th>License Number</th>
<th>Expiration Date</th>
<th>Total Staffing</th>
<th>Locations</th>
<th>Phone Numbers</th>
<th>Addresses</th>
<th>Investigators</th>
</tr>
</thead>
<tbody>
<%For Each item As Entity_Agency In myStaffing%>
<tr>
<!-- Agency Name -->
<td><%Response.Write(item.Name)%></td>
<!-- Account Number -->
<td><%Response.Write(item.Account)%></td>
<!-- Expiration Date -->
<td><%
If item.DateExpiration.ToString() <> "1/1/0001 12:00:00 AM" Then
Response.Write(item.DateExpiration.ToString("MM/dd/yyyy"))
End If
%>
</td>
<!-- Staffing Counts -->
<td align = "center">
<%Response.Write(item.Staffing.Count.ToString())%>
</td>
<!-- Locations -->
<td align = "center">
<% Response.Write(item.Locations)%>
</td>
<!-- Phone Numbers -->
<td align="center">
<% For Each phone As Entity_Agency.Phone In item.Phones%>
<b><%Response.Write(phone.Type)%>:</b> <%Response.Write(phone.Number)%><br />
<% Next %>
</td>
<!-- Addresses -->
<td align="center">
<% For Each address As Entity_Agency.Address In item.Addresses%>
<b><%Response.Write(address.Type)%>:</b>
<%Response.Write(address.Address1)%>
<%If address.Address2.ToString() <> "" Then%>
<%Response.Write(address.Address2)%>
<%End If%>
<%Response.Write(address.City)%> ,
<%Response.Write(address.State)%>
<%Response.Write(address.Zip)%>
<br />
<% Next %> --
</td>
<!-- Staffing-->
<td>
<% For Each person As Entity_Agency.Person In item.Staffing%>
<b><% Response.Write(person.StaffID)%></b>:
<%Response.Write(investigator.LastName)%>
<%Response.Write(investigator.FirstName)%>
<br/>
<% Next%>
</td>
</tr>
<% Next%>
</tbody>
</table>
It looks like you're new here. If you want to get involved, click one of these buttons!
Get useful and friendly help straight from the source.