0

I am getting above error while deserializating BinaryFile to Object. I have tried set position=0, seek.begin method and tried with memory stream but no use. Every time i am getting same issue. Can anybody help me out?. Below is my code snippet for serialization and deserialization

Serialization

Dim fs As New FileStream(Filename, FileMode.OpenOrCreate)
Try

    'fs = New FileStream(Filename, FileMode.OpenOrCreate)
    Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    bf.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full
    bf.TypeFormat = Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded
    bf.Serialize(fs, data)
Catch ex As Exception
    MsgBox("Exception: " & ex.Message & " | Stacktrace: " & ex.StackTrace)
Finally
    fs.Flush()
    fs.Close()
    fs.Dispose()

End Try

Deserialization

Dim fs As FileStream = Nothing

Try
    fs = IO.File.OpenRead(Filename)
    'fs = New FileStream(Filename, FileMode.Open)

    Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()

    bf.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full
    bf.TypeFormat = Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded
    fs.Seek(0, SeekOrigin.Begin) 'fs.Position=0
    Dim obj As Object = bf.Deserialize(fs)
    Return obj
Catch ex As Exception
    MsgBox("There was an exception while trying to convert binary file to object. Exception: " & ex.Message & " | Stacktrace: " & ex.StackTrace)
Finally
    If fs IsNot Nothing Then
        fs.Flush()
        fs.Close()
        fs.Dispose()
    End If
End Try
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Do your objects implement `ISerializable` or do they *just* have the `Serializable` attribute on them? If they implement `ISerializable` please show the serialization / deserialization code ('GetObjectData` and `MyObject(SerializationInfo info, StreamingContext context)`) – Bradley Uffner May 16 '16 at 13:06
  • Don't use `FileMode.OpenOrCreate` when serializing, always use `FileMode.Create` so you ensure that the file is overwritten. – Visual Vincent May 16 '16 at 13:07
  • @BradleyUffner : If it wouldn't, he would get an exception saying that it isn't marked as serializable. – Visual Vincent May 16 '16 at 13:08
  • @VisualVincent not true. You only need to implement ISerializable for custom serialization. you can serialize just fine in most cases with just the attribute. – Bradley Uffner May 16 '16 at 13:08
  • @BradleyUffner : I was talking about if they do not implement _either_. And I now understand that I now missread your comment a bit. I thought you were asking if he used either or. – Visual Vincent May 16 '16 at 13:10
  • What exactly is `data`? It cant be `Object`, yet that is the type you are trying to deserialize *to* – Ňɏssa Pøngjǣrdenlarp May 16 '16 at 15:25
  • @Bradley Uffner: I am not using any Serializable attribute or ISerializable interface, Just using above two methods. Plutonix: I have replaced object with type and Visual Vincent: I have changed OpenOrCreate to Create. Can you please send me standard code snippet for serializing and deserializing – user2845419 May 17 '16 at 05:35
  • Show us the code for whatever class 'data' is. – Bradley Uffner May 17 '16 at 05:37
  • Public Class Document Public Property ID As String Public Property PName As String Public Property Remarks As String Public Property Requirements As New List(Of Requirement) End Class – user2845419 May 17 '16 at 05:41
  • I am using attrivbute – user2845419 May 17 '16 at 05:41

0 Answers0