Going from XML --> LINQ --> Class Object --> .Net Page --> DataTables

Going from XML --> LINQ --> Class Object --> .Net Page --> DataTables

acfredricksacfredricks Posts: 5Questions: 0Answers: 0
edited August 2011 in General
Now added .net project file: http://alex.gizmosurge.com/wp/wp-content/plugins/download-monitor/download.php?id=3


PART 1

Hello,
I was recently tasked to create a VB.NET website (I know...vb...argg!). The goal was to consume xml files on the server and present them to the end user via the website where they can search,print....etc. I ran across DataTables and became instantly in love with them. Here is quick excerpt on what I did. The title of the thread outlines my goals.

[quote]XML - Source Data[/quote]
Consideration: The data object will contain is base information and include multiples of addresses/phones/staff

[code]

Bob's Agency
12341234214
9/4/2011 12:00:00 AM


Mailing
1234 Main Street

New Orleans
LA
70112
New Orleans




Home
(800) xxx-xxxx


Office
(800) xxx-xxxx




0
7549
Peter
Gibbons


1
D0Rk
Bill
Lumbergh



[/code]

[quote]Class Object[/quote]
Consideration: 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.
**I added a Locations property that will get all unique City/State combinations so I can display that on the DataTable for searching capabilities
**If there is a typo then my apologies as I have to edit a few items prior to posting

[code]

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.. 'iterate through each xml address

Dim myAddressItem As Address
myAddressItem.Type = xeAddressItem..Value
myAddressItem.Address1 = xeAddressItem..Value
myAddressItem.Address2 = xeAddressItem..Value
myAddressItem.City = xeAddressItem..Value
myAddressItem.State = xeAddressItem..Value

Dim tempCityState As String = xeAddressItem..Value + ", " + xeAddressItem..Value
If (myLocations.Contains(tempCityState) = False) Then
If (myLocations = "") Then
myLocations = tempCityState
Else
myLocations += "
" + tempCityState 'added
to keep each city/state combo own line
End If
End If

myAddressItem.Zip = xeAddressItem..Value
myAddressItem.County = xeAddressItem..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.. 'iterate through each xml address
Dim myPhoneItem As Phone
myPhoneItem.Type = xePhoneItem..Value
myPhoneItem.Number = xePhoneItem..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.. Order By personXE ..Value Descending

For Each xePersonItem As XElement In myPersonQuery
Dim myPersonItem As Person
myPersonItem.Manager = xePersonItem..Value
myPersonItem.StaffID = xePersonItem..Value
myPersonItem.FirstName = xePersonItem..Value
myPersonItem.LastName = xePersonItem..Value
myStaffList.Add(myPersonItem)
Next

'Put everything into my class object now
myAgencies.Add( _
New Entity_Agency( _
myXE..Value, _
myXE..Value, _
myXE..Value, _
myLocations, _
myAddressList, _
myPhoneList, _
myStaffList))

Next
Return myAgencies
End Function
End Class
[/code]

Replies

  • acfredricksacfredricks Posts: 5Questions: 0Answers: 0
    PART 2

    [quote]Calling .Net Form (CODE BEHIND)[/quote]
    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.

    If you are new to .NET I didn't post the form code auto generated when you create a page. I only put down the code you need to add to the auto generated code to run our methods
    [code]

    '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( )
    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( )
    'Page.DataBind()

    End If
    End Sub

    [/code]
  • acfredricksacfredricks Posts: 5Questions: 0Answers: 0
    edited August 2011
    PART 3

    [quote]Calling .Net Form (HTML)[/quote]

    Consideration: Whether I am using Method 1 or 2 the end product will be an html table that DataTables automatically picks up as long as I pass the table's ID to DataTable. I'm only posting the portion of the html you need to display the values from the data we are wanting to post.

    Goal: Show list of agencies with detailed information/staffing visible on the + click

    [code]
    <!-- 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 -->



    Agency Name
    Account Number
    Expiration Date
    Total Staffing
    Locations
    Phone Numbers <!-- I use the detail option to show this column under the main row -->
    Addresses <!-- I use the detail option to show this column under the main row -->
    Staffing <!-- I use the detail option to show this column under the main row -->











    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Name %>
    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Account %>
    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).DateExpiration.ToString("MM/dd/yyyy")) %
    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Staffing.Count %>
    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency).Locations %>

    <!-- show sublist of phones -->


    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Phone).Type %>
    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Phone).Number %>







    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Address).Type %> 
    <%#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 %>







    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Person).StaffID %>: 
    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Person).LastName %>  
    <%#DirectCast(Container.DataItem, YOURNAMESPACE.Entity_Agency.Person).FirstName %>  










    [/code]
  • acfredricksacfredricks Posts: 5Questions: 0Answers: 0
    edited August 2011
    FINAL PART 4

    [code]

    <!-- METHOD 2 : Response.Write -->
    <!-- Calling data collection object we declared in code behind -->




    Agency Name
    License Number
    Expiration Date
    Total Staffing
    Locations
    Phone Numbers
    Addresses
    Investigators




    <%For Each item As Entity_Agency In myStaffing%>


    <!-- Agency Name -->
    <%Response.Write(item.Name)%>

    <!-- Account Number -->
    <%Response.Write(item.Account)%>

    <!-- Expiration Date -->
    <%
    If item.DateExpiration.ToString() <> "1/1/0001 12:00:00 AM" Then
    Response.Write(item.DateExpiration.ToString("MM/dd/yyyy"))
    End If
    %>


    <!-- Staffing Counts -->

    <%Response.Write(item.Staffing.Count.ToString())%>


    <!-- Locations -->

    <% Response.Write(item.Locations)%>


    <!-- Phone Numbers -->

    <% For Each phone As Entity_Agency.Phone In item.Phones%>
    <%Response.Write(phone.Type)%>: <%Response.Write(phone.Number)%>

    <% Next %>


    <!-- Addresses -->


    <% For Each address As Entity_Agency.Address In item.Addresses%>
    <%Response.Write(address.Type)%>:  
    <%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)%>


    <% Next %> --



    <!-- Staffing-->

    <% For Each person As Entity_Agency.Person In item.Staffing%>

    <% Response.Write(person.StaffID)%>: 
    <%Response.Write(investigator.LastName)%>   
    <%Response.Write(investigator.FirstName)%>   



    <% Next%>


    <% Next%>


    [/code]


    That's all I have. Enjoy!
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Awesome stuff! Thanks so much for sharing that with us all - I'm sure it will be much appreciated by anyone who is getting going with .NET and DataTables!

    I've added a link to your post from the DataTables news feed :-).

    Regards,
    Allan
  • acfredricksacfredricks Posts: 5Questions: 0Answers: 0
    edited August 2011
    Thanks Allan,

    I really enjoy the use of DataTables over .Net grid views. By using both server side .net tech and jQuery for presentation; the customer becomes the real winner
This discussion has been closed.