473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Serialization of an array

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
1 4177
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Franz | last post by:
Let me describe the flow of my program first. 1. Deserialize data from xml file. 2. Addition of "PersonType" class to the AllPersonalData. 3. Serialize data back to the xml file. My question is...
16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
3
by: Ice | last post by:
All - I'm pretty comfortable with simple XML serialization of objects. However I observed something the other day and I wanted to know if I solved it the right way. Basically if I have a...
4
by: ron | last post by:
Hi, I have class object that i serialize using the System.Xml.Serialization class. Intermittently the object is not getting serialized correctly, using System.Xml.Serialization classes....
3
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
11
by: ajou_king | last post by:
I was running some tests on my Win32 1GHZ processor to see how long it would take to transmit objects numerous times via TCP/IP using C# ..NET Remoting vs the C++ trustworthy method of binary...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
5
by: Henry Jones | last post by:
I read a bit on Serialization and came up with the following definition: The System.Runtime.Serialization namespace contains classes that can be used for serializing and deserializing objects....
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.