Serialization question

learning

Active member
Joined
Aug 31, 2009
Messages
28
Location
Michigan
Programming Experience
5-10
A while ago I wrote a program that uses serialization/deserialization to store data files of test results. Seemed like a good idea at the time? Now I'm re-writing the program, starting fresh, and want to read the old data files. I'm running into a problem with the embedded assembly coding in the serialized files. So, even though the class structure in the program has the same signature, it won't load the data and errors with 'Unable to find assembly..'.

I have 2 questions:

1. Can I get around this somehow, even if I have to edit the files to change the embedded assembly information, to read in the existing data?

2. Does anyone use serialization this way, to store individual data files? Or is this just a bad idea.

Thanks in advance for your help/comments.
 
I often use serialization to store or transfer data, and think it is a good idea. With binary serialization you create a dependency on the the assembly that defines the class, it can only be deserialized by the same assembly dependent class definition. So if this has to work between different apps you have to define this in a common class library assembly that all consumers will use. With xml serialization only the data structure matters, so you can define same classes in multiple assemblies and be able to serialize data between them.

From your request it seems you have serialized data binary from one application, thus you get the error when trying to deserilize from a different definition. Options to resolve this is described above, go back to app that serialized it and convert the data (by reading from old format and writing to new) so that it can be deserialized from new app, ie (a) create a common assembly that provides a common definition for binary serialization, or (b) convert by xml serialization.
Option (c) could also be storing data in any other way that enable you to import it in new app.
 
Thanks for your help John. I'm going back in to do some testing now, with this information, and determine the best way to work this out. I really appreciate it.
 
I have a class similar to the following:
VB.NET:
<Serializable()> Public Class TestSampleClass
    Public angle As Array
    Public torque As Array
    Public tension As Array
    Private runDownInformation As String

    Public Sub New(ByVal aData As Array, ByVal tData As Array, ByVal rData As Array)
        angle = aData
        torque = tData
        tension = rData
    End Sub

    Public Property Information As String
        Get
            Return runDownInformation
        End Get
        Set(ByVal value As String)
            runDownInformation = value
        End Set
    End Property

End Class

Once an individual test is run, the data arrays are processed, and all of the information about the run down is recorded a TestSampleClass is created and populated with the data. It is then added to a TestSetClass that contains all of the information about all individual runs performed for the test set :

VB.NET:
<Serializable()> Public Class TestSetClass
    Private testDate As DateTime
    Private lastSave As DateTime
    Private testType As TestType
    Private someInfo(3) As String
    Private partsTested As New ArrayList

    Public Sub New(ByVal tType As TestType)
        testType = tType
        testDate = Date.Now
        lastSaved = Date.Now
    End Sub

    Public Sub New()

    End Sub

    Public Property Tool As String
        Get
            Return someInfo(0)
        End Get
        Set(ByVal value As String)
            someInfo(0) = value
        End Set
    End Property

    Public Property CalDate As String
        Get
            Return someInfo(1)
        End Get
        Set(ByVal value As String)
            someInfo(1) = value
        End Set
    End Property

    Public Property Sensor As String
        Get
            Return someInfo(2)
        End Get
        Set(ByVal value As String)
            someInfo(2) = value
        End Set
    End Property

    Public Property Created() As DateTime
        Get
            Return testDate
        End Get
        Set(ByVal value As DateTime)
            testDate = value
        End Set
    End Property

    Public Property TType() As Integer
        Get
            Return testType
        End Get
        Set(ByVal Value As Integer)
            testType = Value
        End Set
    End Property

    Public Property lastSaved() As DateTime
        Get
            Return lastSave
        End Get
        Set(ByVal Value As DateTime)
            lastSave = Value
        End Set
    End Property

    Public ReadOnly Property Parts(ByVal index As Integer) As TestSampleClass
        Get
            Return partsTested(index)
        End Get
    End Property

    Public Function AddItem(ByVal sample As TestSampleClass) As Boolean
        partsTested.Add(sample)
        Return True
    End Function

End Class

Once a full test is completed I'm trying to save all of the tests information to a file using xmlserialization.

VB.NET:
        Using objStream As StreamWriter = New StreamWriter("C:\testingXML.txt")
            Dim xmlS As New XmlSerializer(aTestSet.GetType)
            xmlS.Serialize(objStream, aTestSet)
            objStream.Close()
        End Using
However, only the data contained in the TestSetClass is saved and none of the data added using the addItem function.

Not sure what I'm doing wrong with this or if using xmlserialization is the correct method to store this type of class. Any help would be appreciated.

Thanks.
 
Your parts collection is inaccessible for the XmlSerializer, you can implement IXmlSerializable to provide custom serialization, or make a simple adjustment to this part:
VB.NET:
    Private partsTested As New ArrayList

    Public ReadOnly Property Parts(ByVal index As Integer) As TestSampleClass
        Get
            Return partsTested(index)
        End Get
    End Property

    Public Function AddItem(ByVal sample As TestSampleClass) As Boolean
        partsTested.Add(sample)
        Return True
    End Function
suggested replacement:
Private partsTested As New List(Of TestSampleClass)

Public ReadOnly Property Parts As List(Of TestSampleClass)
    Get
        Return partsTested
    End Get
End Property

Replace all occurences of type Array with type Object() or narrower.

All classes to be serialized needs a parameterless contructor.
 
Back
Top