Serializing/De-serializing an ArrayList

0.00 avg. rating (0% score) - 0 votes

Declarations:


‘Type of each element in the ArrayList
Public Structure ArrayListEntry

End Structure

‘ArrayList containing elements of above type
Public myArrayList As New ArrayList


Declare a custom serializer:


Dim customSerializer As New XmlSerializer(GetType(Object()), New System.Type() {GetType(ArrayListEntry)})


Methods to serialize data into XML:


A custom serializer must be used.


Public Sub saveSerializedData(ByVal datafile As String, ByVal ConfigObj As Object, ByVal serializer as XmlSerializer) As Boolean
Dim writer As New StreamWriter(datafile)
serializer.Serialize(writer, ConfigObj)
writer.Close()
End Sub


To use the method:


saveSerializedData(“out.xml”, myArrayList.ToArray, customSerializer)


Method to de-serialize data back to Object:


Public Function readSerializedData(ByVal datafile As String, ByRef ConfigType As Type) As Object
Dim retConf As Object = Nothing
Dim serializer As XmlSerializer = New XmlSerializer(ConfigType)
Dim fs As New FileStream(datafile, FileMode.Open)
retConf = serializer.Deserialize(fs)
fs.Close()
Return retConf
End Function


To use the method:


myArrayList.AddRange(CType(readSerializedData(“out.xml”, myArrayList.GetType, isok, customSerializer), Collections.ICollection))

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>