Serialize a collection to XML

Posted on 2006-03-27
Last Modified: 2010-04-23
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


        Dim XML As String = SerializeToText(ColUsers)
    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
            Return m_Title
        End Get
        Set(ByVal value As String)
            m_Title = value
        End Set
    End Property

    Public Property Firstname() As String
            Return m_Firstname
        End Get
        Set(ByVal value As String)
            m_Firstname = value
        End Set
    End Property

    Public Property Surname() As String
            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)
    End Sub

    Default Public ReadOnly Property item(ByVal Index As Integer) As User
                Return CType(Me.item(Index), User)
                Return Nothing
            End Try
        End Get
    End Property
End Class
Question by:Kinger247
    LVL 10

    Author Comment

    Also, I have tried taking some elements from the example at, but still can't get the above working.
    LVL 96

    Accepted Solution

    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.


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now