Serialization of an array

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?
 
C

Craig Vick [MSFT]

Hi Elziko,

My guess is your difficulty isn't so much with serialization as it is writing to and then reading from the xml file. I can't know this for sure because your sample
doesn't show how you created your DataSet and DataTable. Here's a re working of your code that works. Hopefully, you can adapt it to solve your
problem.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testArray As Single() = GetTestArray()
SaveArray(testArray)
End Sub

Private Function GetTestArray() As Single()
Dim testArray(10) As Single
For i As Integer = 0 To 10
testArray(i) = 4.2
Next

Return testArray
End Function

Private Sub SaveArray(ByVal testArray() As Single)
'create memory stream and formatter
Dim msStorage As MemoryStream = New MemoryStream
Dim fStorage As IFormatter = New BinaryFormatter

Dim dsStorage As New DataSet
Dim dtStorage As New DataTable("TestTable")

With dtStorage
Dim col As New DataColumn("TestColumn", GetType(Byte()))
.Columns.Add(col)

End With
dsStorage.Tables.Add(dtStorage)

'serialise array into stream
fStorage.Serialize(msStorage, testArray)

'create byte array from stream
Dim arrB() As Byte = msStorage.ToArray()

'close stream
msStorage.Close()
Dim arrValues() As Object = {arrB}


'fill data table with byte array
dtStorage.Rows.Clear()
dtStorage.Rows.Add(arrValues)

'save to xml file
dsStorage.WriteXml("c:\depot\testArray.xml")
ReadFile()

End Sub

Private Sub ReadFile()
'read xml into data set
Dim dsStorage As New DataSet
Dim dtStorage As New DataTable("TestTable")

With dtStorage
Dim col As New DataColumn("TestColumn", GetType(Byte()))
.Columns.Add(col)
End With
dsStorage.Tables.Add(dtStorage)

dsStorage.ReadXml("c:\depot\testArray.xml")

dsStorage.Tables(0).Columns(0).DataType = GetType(Byte())

'get the bytes stored in the datatable
Dim arrBytes As Byte() = CType(dsStorage.Tables(0).Rows(0).Item(0), Byte())

'create memory stream and formatter
Dim msStorage As MemoryStream = New MemoryStream(arrBytes)
Dim fStorage As IFormatter = New BinaryFormatter

'deserialize to recreate array
msStorage.Position = 0
Dim arrOriginal() As Single = CType(fStorage.Deserialize(msStorage), Single())

'close stream
msStorage.Close()

End Sub

Craig VB.Net Team
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top