Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

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


        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
1 Solution
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.
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.


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now