FileStream and BinaryFormatter

I use FileStream to save my project (Calculation) to hard drive. It works fine. Problem is when I change something in Calculation class that it can not read it again because of different number of items in it. What could I do to make old versions readable?

        BinFormatter.Serialize(FStream, actCalcForm.actCalculation)

Who is Participating?
Alexandre SimõesManager / Technology SpecialistCommented:
Be carefull with the properties types... all must be serializable.

Something like this:

    Public Sub SerializeMyClass(ByVal Class2Ser As mySerClass)

        Dim fs As New System.IO.FileStream(System.Windows.Forms.Application.StartupPath & "\Serialized.xml", IO.FileMode.Create)
        Dim serializer As System.Xml.Serialization.XmlSerializer

        serializer.Serialize(fs, Class2Ser)

    End Sub

    Public Function DeserializeMyClass() As mySerClass

        Dim fs As New System.IO.FileStream(System.Windows.Forms.Application.StartupPath & "\Serialized.xml", IO.FileMode.Open)
        Dim serializer As System.Xml.Serialization.XmlSerializer

        Dim retval As Object = serializer.Deserialize(fs)

        Return retval
    End Function

End Module

<Serializable()> Public Class mySerClass

    Public Sub New()
        'To be serializable, the class must have a contructor with no parameters.
    End Sub

    '-> Only public properties are serialized, sub's and
    '       functions are ignored.
    '-> If you want XML Serializer to ignore a public
    '       property just add <Xml.Serialization.XmlIgnore()>
    '       before the property.
    Private m_P1 As Integer
    Public Property P1() As Integer
            Return P1
        End Get
        Set(ByVal Value As Integer)
            P1 = Value
        End Set
    End Property

End Class
Bob LearnedCommented:
This, I believe, is the inherent problem with serialization, and makes it an unusable form of persistence as far as I am concerned.  Where are you storing this data?  What other information can you give me that would help me make a recommendation?

svemirAuthor Commented:
actually it can be stored where ever user want to. One way to solve this problem is to keep old classes a live inside of application and call it when needed. In that case I need to write additional source code to rewrite informations to new class. Usually it is not a problem because most of inherited classes in main class which I serialize are same. Anyway it seams to me less complicated than old fasion storing data.

I hope that you have better idea about my question.

Thank you anyway.
Alexandre SimõesManager / Technology SpecialistCommented:
I'm sorry (as I had anything to do with it! :)) but you just can't.

Binary serialization doen't support different versions, only XML.
If you have a choice, serialize the Calculation class to XML instead of binaly.

The problem can be the security, XML is pretty much human readable.
To overcome this you can:
1- If you really need a very good protection, ecrypt the XML file, using DES or TDES implementes on the System.Security.Cryptography namespace.
2- If you just want put some camuflage on the file just don't save the file as *.xml, choose another file extension :)

Alex :p
svemirAuthor Commented:
Could you please give some example of serialization to XML because once I try and it doesn't work.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.