Kinger247
asked on
Serialize a collection to XML
Hi there,
I'm trying to serialize a collection, but can't get it to work. I've searched Google high and low but cannot find any working examples.
I've included the code (below), all I'm trying to do is create a collection of 10 (for example) items, the serialize it to XML …
this is really bugging me because I know it'll be something stupid I've missed !
thanks in advance.
'// THE CODE ////////////////////////// /////////
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Form1
Public ColUsers As New UserCollection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TMPUser As User
For iCounter As Integer = 1 To 5
TMPUser = New User
With TMPUser
.Title = "Tenant" & iCounter.ToString
.Firstname = "Project" & iCounter.ToString
.Surname = "Team" & iCounter.ToString
End With
ColUsers.add(TMPUser)
Next
Dim XML As String = SerializeToText(ColUsers)
Console.WriteLine(XML)
End Sub
Public Function SerializeToText(ByVal obj As Object) As System.String
Dim StringWriter As New System.IO.StringWriter
Dim XmlSerializer As New System.XML.Serialization.X mlSerializ er(obj.Get Type)
XmlSerializer.Serialize(St ringWriter , obj)
Return StringWriter.ToString
End Function
End Class
<Serializable()> _
Public Class User
Private m_Title As String
Private m_Firstname As String
Private m_Surname As String
Public Property Title() As String
Get
Return m_Title
End Get
Set(ByVal value As String)
m_Title = value
End Set
End Property
Public Property Firstname() As String
Get
Return m_Firstname
End Get
Set(ByVal value As String)
m_Firstname = value
End Set
End Property
Public Property Surname() As String
Get
Return m_Surname
End Get
Set(ByVal value As String)
m_Surname = value
End Set
End Property
End Class
<Serializable()> _
Public Class UserCollection
Inherits CollectionBase
Public Sub add(ByVal item As User)
Me.List.Add(item)
End Sub
Default Public ReadOnly Property item(ByVal Index As Integer) As User
Get
Try
Return CType(Me.item(Index), User)
Catch
Return Nothing
End Try
End Get
End Property
End Class
I'm trying to serialize a collection, but can't get it to work. I've searched Google high and low but cannot find any working examples.
I've included the code (below), all I'm trying to do is create a collection of 10 (for example) items, the serialize it to XML …
this is really bugging me because I know it'll be something stupid I've missed !
thanks in advance.
'// THE CODE //////////////////////////
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Form1
Public ColUsers As New UserCollection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TMPUser As User
For iCounter As Integer = 1 To 5
TMPUser = New User
With TMPUser
.Title = "Tenant" & iCounter.ToString
.Firstname = "Project" & iCounter.ToString
.Surname = "Team" & iCounter.ToString
End With
ColUsers.add(TMPUser)
Next
Dim XML As String = SerializeToText(ColUsers)
Console.WriteLine(XML)
End Sub
Public Function SerializeToText(ByVal obj As Object) As System.String
Dim StringWriter As New System.IO.StringWriter
Dim XmlSerializer As New System.XML.Serialization.X
XmlSerializer.Serialize(St
Return StringWriter.ToString
End Function
End Class
<Serializable()> _
Public Class User
Private m_Title As String
Private m_Firstname As String
Private m_Surname As String
Public Property Title() As String
Get
Return m_Title
End Get
Set(ByVal value As String)
m_Title = value
End Set
End Property
Public Property Firstname() As String
Get
Return m_Firstname
End Get
Set(ByVal value As String)
m_Firstname = value
End Set
End Property
Public Property Surname() As String
Get
Return m_Surname
End Get
Set(ByVal value As String)
m_Surname = value
End Set
End Property
End Class
<Serializable()> _
Public Class UserCollection
Inherits CollectionBase
Public Sub add(ByVal item As User)
Me.List.Add(item)
End Sub
Default Public ReadOnly Property item(ByVal Index As Integer) As User
Get
Try
Return CType(Me.item(Index), User)
Catch
Return Nothing
End Try
End Get
End Property
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER