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.XmlSerializer(obj.GetType)
        XmlSerializer.Serialize(StringWriter, 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
LVL 10
Kinger247Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kinger247Author Commented:
Also, I have tried taking some elements from the example at http://www.codeproject.com/vb/net/escSerializedCollection.asp, but still can't get the above working.
0
Bob LearnedCommented:
I am not a big fan of serialization, because if you serialize an object, write the file, and then change a property, the object can no longer be serialized, so you have to have complicated ways of handling it.

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.