• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

FileStream and BinaryFormatter

Hello!
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)

Thanks!
0
svemir
Asked:
svemir
  • 2
  • 2
1 Solution
 
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?

Bob
0
 
svemirAuthor Commented:
Well,
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.
0
 
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
0
 
svemirAuthor Commented:
Could you please give some example of serialization to XML because once I try and it doesn't work.
Thanks!
0
 
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)

        fs.Close()
    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)
        fs.Close()

        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
        Get
            Return P1
        End Get
        Set(ByVal Value As Integer)
            P1 = Value
        End Set
    End Property

End Class
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now