I'm trying a simple object serialization and deserialization , and keep
getting this error:
System.Runtime. Serialization.S erializationExc eption: Binary stream does not
contain a valid BinaryHeader, 0 possible causes, invalid stream or object
version change between serialization and deserialization .
Here's my code. it does nothing but to serialize a DataTable object into a
byte array, and then read the byte array back for deserialization . I
verified that the byte array length did not change. the error occurs on the
object newObj = formatter.Deser ialize(ms2); line.
public class Test {
Public Test {
DataTable dt = this.CreateData Source();
System.IO.Strea m ms= new System.IO.Memor yStream();
IFormatter formatter = new BinaryFormatter ();
formatter.Seria lize(ms, dt);
int numBytesToRead = (int) ms.Length;
byte[] bytes = new byte[numBytesToRead];
int numBytesRead = 0;
int n = ms.Read(bytes, numBytesRead, numBytesToRead) ;
ms.Position = 0;
ms.Close();
System.IO.Strea m ms2= new System.IO.Memor yStream();
int numBytesToWrite = bytes.Length;
ms2.SetLength(n umBytesToWrite) ;
int numBytesWritten = 0;
ms2.Write(bytes , numBytesWritten , numBytesToWrite );
ms2.Position = 0;
object newObj = formatter.Deser ialize(ms2);
ms2.Close();
}
private DataTable CreateDataSourc e() {
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add( new DataColumn("Int egerValue", typeof(Int32))) ;
dt.Columns.Add( new DataColumn("Str ingValue", typeof(string)) );
dt.Columns.Add( new DataColumn("Cur rencyValue", typeof(double)) );
for (int i = 0; i < 10; i++) {
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i+1);
dt.Rows.Add(dr) ;
}
return dt;
}
}
getting this error:
System.Runtime. Serialization.S erializationExc eption: Binary stream does not
contain a valid BinaryHeader, 0 possible causes, invalid stream or object
version change between serialization and deserialization .
Here's my code. it does nothing but to serialize a DataTable object into a
byte array, and then read the byte array back for deserialization . I
verified that the byte array length did not change. the error occurs on the
object newObj = formatter.Deser ialize(ms2); line.
public class Test {
Public Test {
DataTable dt = this.CreateData Source();
System.IO.Strea m ms= new System.IO.Memor yStream();
IFormatter formatter = new BinaryFormatter ();
formatter.Seria lize(ms, dt);
int numBytesToRead = (int) ms.Length;
byte[] bytes = new byte[numBytesToRead];
int numBytesRead = 0;
int n = ms.Read(bytes, numBytesRead, numBytesToRead) ;
ms.Position = 0;
ms.Close();
System.IO.Strea m ms2= new System.IO.Memor yStream();
int numBytesToWrite = bytes.Length;
ms2.SetLength(n umBytesToWrite) ;
int numBytesWritten = 0;
ms2.Write(bytes , numBytesWritten , numBytesToWrite );
ms2.Position = 0;
object newObj = formatter.Deser ialize(ms2);
ms2.Close();
}
private DataTable CreateDataSourc e() {
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add( new DataColumn("Int egerValue", typeof(Int32))) ;
dt.Columns.Add( new DataColumn("Str ingValue", typeof(string)) );
dt.Columns.Add( new DataColumn("Cur rencyValue", typeof(double)) );
for (int i = 0; i < 10; i++) {
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i+1);
dt.Rows.Add(dr) ;
}
return dt;
}
}
Comment