locked
Problem Deserializing Binary File RRS feed

  • Question

  • I have an array of Structure (Person) which I serialized and formatted as follows

     

      <Serializable()> Structure Person

            Public strID As String

            Public strName As String

            Public strReport As String

            Public strAttend As String

     

            Public Shared Widening Operator CType(v As Person) As IO.MemoryStream

                Try

                    Throw New NotImplementedException()

                Catch ex As Exception

                    MsgBox("Failed to deserialise." + Chr(13) + "Reason: " & ex.Message)

                End Try

            End Operator

        End Structure

     

    Public Student(35) As Person

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

    Dim ms as New System.IO.MemorySteam()

     

    bf.Serialize(ms,Student(count))

    My.Computer.FileSystem.WriteAllBytes(strFile1,ms.GetBuffer(),True)

     

    The file is created and populated as desired.  When I check it with WordPad all records are present.

    When I deserialize it, as below, I am only seeing the first record repeated.  I am thinking either the pointer is not moving or I am going back to record 1 on each iteration.  What am I missing?

     

    Public Student(35) As Person

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

    Dim ms as New System.IO.MemorySteam()

    Dim bytes As Byte() = My.Computer.FileSystem.ReadAllBytes(strFile1)

     

    My.Computer.FileSystem.ReadAllBytes(strFile1)

    ms.Seek(0,SeekOrigin.Begin)

     

    For i = 0 to 19

        Student(i) = DirectCast(bf.Deserialize(New MemoryStream(bytes)),Person)

    Next

     

    Thank you, in advance, for any help or suggestions you may offer.

    Thursday, June 22, 2017 3:42 PM