E
elziko
My intention is to store an array of singles inside a DataTable so that it
can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't
know yet so I'm just saving it as an XML file for testing. Here are my two
procedures for saving the array and loading it abck in again.
Private Sub Save
'create memory stream and formatter
Dim msStorage As MemoryStream = New MemoryStream
Dim fStorage As IFormatter = New BinaryFormatter
'serialise array into stream
fStorage.Serialize(msStorage, sngArray)
'create byte array from stream
Dim arrB(Convert.ToInt32(msStorage.Length) - 1) As Byte
msStorage.Seek(0, SeekOrigin.Begin)
msStorage.Read(arrB, 0, Convert.ToInt32(msStorage.Length) - 1)
'close stream
msStorage.Close()
'fill data table with byte array
dtStorage.Rows.Clear()
Dim arrValues(1) As Object
arrValues(0) = 1
arrValues(1) = arrB
dtStorage.Rows.Add(arrValues)
'save to xml file
dsStorage.WriteXml("c:\test.xml")
End Sub
Private Sub Load
'read xml into data set
dsStorage.ReadXml("c:\test.xml")
'get the bytes stored in the datatable
Dim arrBytes As Byte() = CType(dtStorage.Rows(0).Item(1), Byte())
'create memory stream and formatter
Dim msStorage As MemoryStream = New MemoryStream
Dim fStorage As IFormatter = New BinaryFormatter
'write the bytes to the stream
msStorage.Write(arrBytes, 0, arrBytes.Length)
msStorage.Flush()
'deserialize to recreate array
msStorage.Position = 0
Dim arrOriginal As Single(,) = CType(fStorage.Deserialize(msStorage),
Single(,)) '***
'close stream
msStorage.Close()
End Sub
However, when trying to reload the array, on the line commented with *** I
get the following exception:
An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: Binary stream does not contain a valid BinaryHeader,
0 possible causes, invalid stream or object version change between
serialization and deserialization.
I have made sure to call flush after writing to the stream and to reset the
position of the sream before deserialising. SO, what have I done wrong and
there is there a much better way of doing what I'm trying to do?
can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't
know yet so I'm just saving it as an XML file for testing. Here are my two
procedures for saving the array and loading it abck in again.
Private Sub Save
'create memory stream and formatter
Dim msStorage As MemoryStream = New MemoryStream
Dim fStorage As IFormatter = New BinaryFormatter
'serialise array into stream
fStorage.Serialize(msStorage, sngArray)
'create byte array from stream
Dim arrB(Convert.ToInt32(msStorage.Length) - 1) As Byte
msStorage.Seek(0, SeekOrigin.Begin)
msStorage.Read(arrB, 0, Convert.ToInt32(msStorage.Length) - 1)
'close stream
msStorage.Close()
'fill data table with byte array
dtStorage.Rows.Clear()
Dim arrValues(1) As Object
arrValues(0) = 1
arrValues(1) = arrB
dtStorage.Rows.Add(arrValues)
'save to xml file
dsStorage.WriteXml("c:\test.xml")
End Sub
Private Sub Load
'read xml into data set
dsStorage.ReadXml("c:\test.xml")
'get the bytes stored in the datatable
Dim arrBytes As Byte() = CType(dtStorage.Rows(0).Item(1), Byte())
'create memory stream and formatter
Dim msStorage As MemoryStream = New MemoryStream
Dim fStorage As IFormatter = New BinaryFormatter
'write the bytes to the stream
msStorage.Write(arrBytes, 0, arrBytes.Length)
msStorage.Flush()
'deserialize to recreate array
msStorage.Position = 0
Dim arrOriginal As Single(,) = CType(fStorage.Deserialize(msStorage),
Single(,)) '***
'close stream
msStorage.Close()
End Sub
However, when trying to reload the array, on the line commented with *** I
get the following exception:
An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: Binary stream does not contain a valid BinaryHeader,
0 possible causes, invalid stream or object version change between
serialization and deserialization.
I have made sure to call flush after writing to the stream and to reset the
position of the sream before deserialising. SO, what have I done wrong and
there is there a much better way of doing what I'm trying to do?