5,426,531 members and growing! (15,283 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Advanced License: The Code Project Open License (CPOL)

Serializable Generic Collection

By behnam usefy

A Generic Collection that could be serialized in Xml format
VB 8.0, VB 9.0, VB, Windows, .NET, .NET 3.0, .NET 3.5, .NET 2.0, ASP.NET, VS2005, VS2008, Visual Studio, Dev

Posted: 10 Nov 2007
Updated: 27 Nov 2007
Views: 7,230
Bookmarked: 12 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 1.70 Rating: 2.82 out of 5
2 votes, 50.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

By default, Generics Collections could not be serialize from xmlserializer. I try to create a generic collection that can serialize 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 want to made XmlSerializable object we must Implement it from IXmlSerializable. and then we must implement 3 methods (GetSchema,ReadXml,WriteXml). In GetSchema we must return a string that contain XML Schema about this object (We Leave it blank in this version!). ReadXml method will contain statements that deserialize Xml to Objects and finaly 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
                reader.Read()
                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), _
                                                   Nothing)
                            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), _
                                                   Nothing)


                            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), _
                                           Nothing)

                            Else
                                pc(i).SetValue(ti, _
                                Convert.ChangeType( _
                                reader.ReadElementString(pc(i).Name), pc(i).PropertyType), _
                                    Nothing)

                            End If

                        End If

                    Next
                    Me.Add(ti)

                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)
            writer.WriteStartElement(GetType(T).Name)
            For j As Int32 = 0 To pc.Length - 1
                If pc(j).CanRead And pc(j).CanWrite Then
                    writer.WriteStartElement(pc(j).Name)
                    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)))

                    Else
                        writer.WriteString(pc(j).GetValue(ti, Nothing).ToString())

                    End If
                    writer.WriteEndElement()

                End If

            Next
            writer.WriteEndElement()

        Next

    End Sub

        

Using the code

As normaly use of Generic Collections :

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

I suggest that inherit from this class. Our collection can now deserialize simple properties like string,integer and Arrays and Obects that inherits from ICollection.

ToDo

Support serialization for Hashtables, Enums and ....

History

Version 0.2 : Support for Array and ICollection, Fixed some bugs. 28/11/2007

Version 0.1 : Initial Release 11/11/2007

License

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

About the Author

behnam usefy



Occupation: Web Developer
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Nov 2007
Editor:
Copyright 2007 by behnam usefy
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project