Click here to Skip to main content
12,818,489 members (34,845 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 10 Nov 2007

Serializable Generic Collection

, 27 Nov 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A Generic collection that can be serialized in XML format.


By default, Generic Collections can not be serialized using an XML serializer. I am trying to create a generic collection that can be serialized in XML format.

Public Class CollectionBase(Of T)
        Inherits System.Collections.ObjectModel.Collection(Of T)
        Implements System.Xml.Serialization.IXmlSerializable

About this Collection

When we implement an XmlSerializable object, we must inherit it from IXmlSerializable. And then, we must implement three methods: GetSchema, ReadXml, WriteXml. In GetSchema, we must return a string that contains the XML schema about the object (we leave it blank in this version!). The ReadXml method will contain statements that deserialize the XML to objects, and finally in WriteXml, we serialize our collection into XML.

Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) _
           Implements System.Xml.Serialization.IXmlSerializable.ReadXml
    Dim pc() As PropertyInfo = GetType(T).GetProperties()
    While reader.Read()
        If reader.Name = GetType(T).Name Then
            If pc.Length > 0 Then
                Dim ti As T = GetInstance()
                For i As Int32 = 0 To pc.Length - 1
                    If pc(i).CanRead And pc(i).CanWrite Then
                        Dim st As SerilalizeType = GetSerilalizeType(pc(i).PropertyType)
                        If st = SerilalizeType.Complex Then
                            Dim o As Object = GetInstance(pc(i).PropertyType)
                            DesrializeObject(reader, o, pc(i).Name)
                            pc(i).SetValue(ti, _
                                           Convert.ChangeType(o, pc(i).PropertyType), _
                        ElseIf st = SerilalizeType.Guid Then
                            Dim strGuid As String = reader.ReadElementString(pc(i).Name)
                            Dim newId As Guid = New Guid(strGuid)
                            pc(i).SetValue(ti, New Guid(strGuid), Nothing)

                        ElseIf st = SerilalizeType.Array Then
                            Dim o As Object = Nothing
                            DesrializeArray(reader, o, pc(i).PropertyType)
                            pc(i).SetValue(ti, _
                                    Convert.ChangeType(o, pc(i).PropertyType), _

                        ElseIf st = SerilalizeType.ICollection Then
                            Dim o As Object = Nothing
                            DesrializeCollection(reader, o, pc(i).PropertyType)
                            pc(i).SetValue(ti, _
                                   Convert.ChangeType(o, pc(i).PropertyType), _
                            pc(i).SetValue(ti, _
                            Convert.ChangeType( _
                            reader.ReadElementString(pc(i).Name), pc(i).PropertyType), _
                        End If
                    End If
            End If
        End If
    End While
End Sub

Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) _
        Implements System.Xml.Serialization.IXmlSerializable.WriteXml
    Dim pc() As PropertyInfo = GetType(T).GetProperties()
    Dim ti As T = Nothing
    For i As Int32 = 0 To Me.Items.Count - 1
        ti = Me.Item(i)
        For j As Int32 = 0 To pc.Length - 1
            If pc(j).CanRead And pc(j).CanWrite Then
                Dim st As SerilalizeType = GetSerilalizeType(pc(j).PropertyType)
                If st = SerilalizeType.Complex Or _
                   st = SerilalizeType.Array Or _
                   st = SerilalizeType.ICollection Then
                    writer.WriteRaw(SerializeObject(pc(j).GetValue(ti, Nothing)))
                    writer.WriteString(pc(j).GetValue(ti, Nothing).ToString())
                End If
            End If
End Sub

Using the Code

As is usual, here is how we use Generic Collections:

Dim MyCol As New DNE.Components.CollectionBase(Of SomeObject)

I suggest inheriting your collection from this class. Our collection can now deserialize simple properties like string, integer, and arrays and objects that inherits from ICollection.


Support serialization for Hashtables, Enums, and ....


  • Version 0.2: Support for Array and ICollection, fixed some bugs. 28/11/2007.
  • Version 0.1: Initial release. 11/11/2007.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Behnam Yousefi
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGeneric collections can be serialized using XmlSerializer Pin
Yingbiao13-Dec-09 19:28
memberYingbiao13-Dec-09 19:28 
Generic collections can be serialized using XmlSerializer.
        public static string SerializeObjectToXml<T>(T objectToBeSerialized)
            StringBuilder sb = new StringBuilder();

            StringWriter sw = new StringWriter(sb);
            XmlSerializer serializer = new XmlSerializer(objectToBeSerialized.GetType());
            serializer.Serialize(sw, objectToBeSerialized);

            return sb.ToString();

        public static T DeserializeObject<T>(string objectXml) where T: class
            StringReader sr = new StringReader(objectXml);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return serializer.Deserialize(sr) as T;

You could use the above helper functions to serialize and deserialize any object that is marked as Serializable including Collection<T>.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 28 Nov 2007
Article Copyright 2007 by Behnam Yousefi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid