By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Serialization of an array

P: n/a
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.SerializationExcepti on' 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?

--

Cheers,

elziko
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
--------------------------------------------------------------------
This reply is provided AS IS, without warranty (express or implied).

--------------------
From: "elziko" <el****@NOTSPAMMINGyahoo.co.uk>
Subject: Serialization of an array
Date: Wed, 7 Jul 2004 13:59:13 +0100
Lines: 83
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
Message-ID: <ux**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 217.206.100.82
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:214697
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

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.SerializationExcept ion' 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?

--

Cheers,

elziko

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.