|
|
Using ADO.NET to Serialize and Deserialize ListBox and ListView Controls to XML Files | | With ADO.NET, it is a simple task to Serialize (save the contents of a ListBox or ListView) to an XML file, and later DeSerialize (reload the controls) from the XML file.
I wrote a previous article on this subject, but since then I have created a reusable class to Serialize the contents of either a ListBox or a ListView to an XML file. In several applications, I have found the need to save the contents of one of these types of controls because the application allows the user to dynamically add to or delete from the contents of these controls at run-time.
To Serialize, or save the contents of a ListView, use the following code. I am passing the ListView to be serialized and the filename of the XML file in which to save the ListView contents.
Dim o As New CControlSerializer
o.SerializeControl(Me.lvRegions, CShare.NetRegionsFile)
|
To Deserialize or reload the ListView from the XML file, use the following code.
o.DeSerializeControl(Me.lvRegions, CShare.NetRegionsFile)
CShare.rdt = o.LatestDataTable
o.DeSerializeControl(Me.lbClassRegions, CShare.CodeElementsFile)
|
If you need to get the DataTable, so that you can use the contents instead of a Collection, in another way in your application, the Class provides a Public property named LatestDataTable. This DataTable will have the contents of the control that was last serialized or deserialized by the class. You can access that DataTable by using the following code. CShare is a Shared Class with a public variable "rdt" that requires a pointer to the DataTable.
o.DeSerializeControl(Me.lvRegions, CShare.NetRegionsFile)
CShare.rdt = o.LatestDataTable
|
The VB.NET Code for this class is shown below.
Figure 1 - VB.NET Class Code.
Imports System.Windows.Forms
Public Class CControlSerializer
' This datatable will be the last datatable created
' by the deserialize method and is provided for use
' by the caller in case they want access to the datatable
' to use in the app instead of a collection of the data
' deserailized to the control
Public LatestDataTable As DataTable
' Returns a datatable of specified xml file.
Public Sub GetDatatable(ByVal fn As String, ByRef dt As DataTable)
Dim ds As New DataSet
Try
If Not IO.File.Exists(fn) Then
Exit Sub
End If
ds.ReadXml(fn)
dt = ds.Tables("DT")
Catch ex As System.Exception
StructuredErrorHandler(ex)
End Try
End Sub
' Save the contents of the listview to the xmlfile
Public Overloads Sub SerializeControl(ByRef lv As ListView, ByVal fn As String)
Dim ds As New DataSet
Dim i As Integer
Dim j As Integer
Try
Dim dt As DataTable = ds.Tables.Add("DT")
For i = 0 To lv.Columns.Count - 1
dt.Columns.Add("Col" & i.ToString, Type.GetType("System.String"))
Next
For i = 0 To lv.Items.Count - 1
With lv.Items(i)
Dim al As New ArrayList
For j = 0 To lv.Items(i).SubItems.Count - 1
al.Add(.SubItems(j).Text)
Next j
AddRowToTable(dt, al)
End With
Next i
ds.WriteXml(fn)
Catch ex As System.Exception
StructuredErrorHandler(ex.ToString)
End Try
End Sub
' Save the contents of the listbox to the xmlfile
Public Overloads Sub SerializeControl(ByRef lb As ListBox, ByVal fn As String)
Dim ds As New DataSet
Dim i As Integer
Dim j As Integer
Try
Dim dt As DataTable = ds.Tables.Add("DT")
dt.Columns.Add("Col1", Type.GetType("System.String"))
For i = 0 To lb.Items.Count - 1
Dim al As New ArrayList
al.Add(lb.Items(i))
AddRowToTable(dt, al)
Next i
ds.WriteXml(fn)
Catch ex As System.Exception
StructuredErrorHandler(ex.ToString)
End Try
End Sub
' Reloads passed ListView from passed XML filename.
Public Overloads Sub DeSerializeControl(ByRef lv As ListView, ByVal fn As String)
Dim ds As New DataSet
Try
If Not IO.File.Exists(fn) Then
Exit Sub
End If
ds.ReadXml(fn)
Dim dt As DataTable = ds.Tables("DT")
Dim i As Integer
Dim j As Integer
lv.Items.Clear()
LatestDataTable = dt
For i = 0 To dt.Rows.Count - 1
Dim dr As DataRow = dt.Rows(i)
With lv
.Items.Add(dr(0))
For j = 1 To dt.Columns.Count - 1
.Items(i).SubItems.Add(dr(j))
Next
End With
Next
Catch ex As System.Exception
StructuredErrorHandler(ex)
End Try
End Sub
' Reloads passed ListBox from passed XML filename.
Public Overloads Sub DeSerializeControl(ByRef lb As ListBox, ByVal fn As String)
Dim ds As New DataSet
Try
If Not IO.File.Exists(fn) Then
Exit Sub
End If
ds.ReadXml(fn)
Dim dt As DataTable = ds.Tables("DT")
Dim i As Integer
Dim j As Integer
lb.Items.Clear()
LatestDataTable = dt
For i = 0 To dt.Rows.Count - 1
Dim dr As DataRow = dt.Rows(i)
With lb
.Items.Add(dr(0))
End With
Next
Catch ex As System.Exception
StructuredErrorHandler(ex)
End Try
End Sub
' Add the row represented by the passed arrarlist to the passed datatable.
Public Function AddRowToTable(ByRef dt As DataTable, ByRef al As ArrayList) As Boolean
Dim i As Short
Try
Dim newRow As DataRow = dt.NewRow
For i = 0 To al.Count - 1
' add a row to the passed dtList
newRow(i) = al(i)
Next
dt.Rows.Add(newRow)
Return True
Catch ex As System.Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
End Class
| |
|