473,326 Members | 2,013 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,326 software developers and data experts.

serialization and deserialization problem

Hi i am serializing a 'ref struct' object as follows :

private: void Seri( String ^path, Object^ obj )
{
FileStream^ fileStrm ;
try
{
//Serialize entire object into Binary stream
fileStrm = File::Open( path , FileMode::Create );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
binFormatter->Serialize(fileStrm, obj );
}
finally
{
if (fileStrm != nullptr )
{
fileStrm->Flush();
fileStrm->Close();
}
}
}

private: Object ^ Desi(String ^path)
{
FileStream ^fileStream ;
Object ^obj ;
try
{
fileStream = File::Open(path ,FileMode::Open );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
obj = binFormatter->Deserialize(fileStream);
}
finally
{
if (fileStream != nullptr )
{
fileStream->Close();
}
}
return obj;

}

// now i am serializing it in follwing way...

WayPoint2 ^waypoint1 = gcnew WayPoint2( gcnew LatLog(41.0,-75.0),
"First","Description", DateTime::Now.AddHours( -10) ,
DateTime::Now.AddHours( -8) ) ;

Seri( "a.waypoint", waypoint1 );

//Serialization works fine.... but following deserialization throws me
exception...

WayPoint2 ^waypoint = ( WayPoint2 ^) this->Desi("a.waypoint") ;

// Exception :
'System.Runtime.Serialization.SerializationExcepti on' occurred in
mscorlib.dll
Additional information: Binary stream '169' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.

// The definition of struct WayPoint2 is as follows
[Serializable]
public ref struct WayPoint2
{
public :
LatLog ^LatLong;
String ^Name;
String ^Description;
DateTime ^Arrival;
DateTime ^Departure;
WayPoint2()
{
this->LatLong = gcnew LatLog();
this->Name = String::Empty ;
this->Description = String::Empty ;
this->Arrival = DateTime::Now ;
this->Departure = DateTime::Now ;
}

WayPoint2 ( LatLog ^latlog, String ^name, String ^description,
DateTime ^arrival, DateTime ^departure)
{
this->LatLong = latlog;
this->Name = name ;
this->Description = description ;
this->Arrival = arrival ;
this->Departure = departure ;
}
}

Can any one help me to sesolve this problem? i have taken care of
Flushing, null references; still its not working.
[Ankit Jain]
http://www.ankitjain.info/ankit/

Nov 17 '05 #1
3 2036
I am working in Managed C++ .NET Framework RC1

Nov 17 '05 #2
DateTime is a value type, remove the ^ from...

DateTime Arrival;
DateTime Departure;

WayPoint2 ( LatLog ^latlog, String ^name, String ^description, DateTime
arrival, DateTime departure)

Willy.
"AnkitAsDeveloper [Ankit]" <an**************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi i am serializing a 'ref struct' object as follows :

private: void Seri( String ^path, Object^ obj )
{
FileStream^ fileStrm ;
try
{
//Serialize entire object into Binary stream
fileStrm = File::Open( path , FileMode::Create );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
binFormatter->Serialize(fileStrm, obj );
}
finally
{
if (fileStrm != nullptr )
{
fileStrm->Flush();
fileStrm->Close();
}
}
}

private: Object ^ Desi(String ^path)
{
FileStream ^fileStream ;
Object ^obj ;
try
{
fileStream = File::Open(path ,FileMode::Open );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
obj = binFormatter->Deserialize(fileStream);
}
finally
{
if (fileStream != nullptr )
{
fileStream->Close();
}
}
return obj;

}

// now i am serializing it in follwing way...

WayPoint2 ^waypoint1 = gcnew WayPoint2( gcnew LatLog(41.0,-75.0),
"First","Description", DateTime::Now.AddHours( -10) ,
DateTime::Now.AddHours( -8) ) ;

Seri( "a.waypoint", waypoint1 );

//Serialization works fine.... but following deserialization throws me
exception...

WayPoint2 ^waypoint = ( WayPoint2 ^) this->Desi("a.waypoint") ;

// Exception :
'System.Runtime.Serialization.SerializationExcepti on' occurred in
mscorlib.dll
Additional information: Binary stream '169' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.

// The definition of struct WayPoint2 is as follows
[Serializable]
public ref struct WayPoint2
{
public :
LatLog ^LatLong;
String ^Name;
String ^Description;
DateTime ^Arrival;
DateTime ^Departure;
WayPoint2()
{
this->LatLong = gcnew LatLog();
this->Name = String::Empty ;
this->Description = String::Empty ;
this->Arrival = DateTime::Now ;
this->Departure = DateTime::Now ;
}

WayPoint2 ( LatLog ^latlog, String ^name, String ^description,
DateTime ^arrival, DateTime ^departure)
{
this->LatLong = latlog;
this->Name = name ;
this->Description = description ;
this->Arrival = arrival ;
this->Departure = departure ;
}
}

Can any one help me to sesolve this problem? i have taken care of
Flushing, null references; still its not working.
[Ankit Jain]
http://www.ankitjain.info/ankit/

Nov 17 '05 #3
Thankx for replying.

The problem is solved. The deserialization works for value type
DateTime object. And when we create reference type object of DateTime
it thows SerializationException.

But still a question is roaming in my mind is why it allows to create
reference type objects even though it is defined as 'public sealed
struct DateTime', ie value type ?

[Ankit Jain]
http://www.ankitjain.info/ankit/
Willy Denoyette [MVP] wrote:
DateTime is a value type, remove the ^ from...

DateTime Arrival;
DateTime Departure;

WayPoint2 ( LatLog ^latlog, String ^name, String ^description, DateTime
arrival, DateTime departure)

Willy.
"AnkitAsDeveloper [Ankit]" <an**************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi i am serializing a 'ref struct' object as follows :

private: void Seri( String ^path, Object^ obj )
{
FileStream^ fileStrm ;
try
{
//Serialize entire object into Binary stream
fileStrm = File::Open( path , FileMode::Create );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
binFormatter->Serialize(fileStrm, obj );
}
finally
{
if (fileStrm != nullptr )
{
fileStrm->Flush();
fileStrm->Close();
}
}
}

private: Object ^ Desi(String ^path)
{
FileStream ^fileStream ;
Object ^obj ;
try
{
fileStream = File::Open(path ,FileMode::Open );
BinaryFormatter^ binFormatter = gcnew BinaryFormatter();
obj = binFormatter->Deserialize(fileStream);
}
finally
{
if (fileStream != nullptr )
{
fileStream->Close();
}
}
return obj;

}

// now i am serializing it in follwing way...

WayPoint2 ^waypoint1 = gcnew WayPoint2( gcnew LatLog(41.0,-75.0),
"First","Description", DateTime::Now.AddHours( -10) ,
DateTime::Now.AddHours( -8) ) ;

Seri( "a.waypoint", waypoint1 );

//Serialization works fine.... but following deserialization throws me
exception...

WayPoint2 ^waypoint = ( WayPoint2 ^) this->Desi("a.waypoint") ;

// Exception :
'System.Runtime.Serialization.SerializationExcepti on' occurred in
mscorlib.dll
Additional information: Binary stream '169' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.

// The definition of struct WayPoint2 is as follows
[Serializable]
public ref struct WayPoint2
{
public :
LatLog ^LatLong;
String ^Name;
String ^Description;
DateTime ^Arrival;
DateTime ^Departure;
WayPoint2()
{
this->LatLong = gcnew LatLog();
this->Name = String::Empty ;
this->Description = String::Empty ;
this->Arrival = DateTime::Now ;
this->Departure = DateTime::Now ;
}

WayPoint2 ( LatLog ^latlog, String ^name, String ^description,
DateTime ^arrival, DateTime ^departure)
{
this->LatLong = latlog;
this->Name = name ;
this->Description = description ;
this->Arrival = arrival ;
this->Departure = departure ;
}
}

Can any one help me to sesolve this problem? i have taken care of
Flushing, null references; still its not working.
[Ankit Jain]
http://www.ankitjain.info/ankit/


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on...
4
by: Jeff T. | last post by:
Hello, I have an existing set of C# classes that encapsulate our application data. They are in a heirachy with each subclass defining more specific types of data. I would like to serialize these...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
1
by: Maheal | last post by:
I have been trying to Serialize an object that is the child of another object which is also serializable. Here is the simplified scenario (assume missing code is correct): class One :...
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...
5
by: Perecli Manole | last post by:
I have a class that has been serialized and saved to disk. I am trying to deserialize it back into the same class which now has an extra private member. It will not deserialize because its...
0
by: Goethals Frederik | last post by:
Hi, I have some questions that are a little difficult to explain, so I give it a try... I have an application (aSP.NET with VB.NET codebehind) and I would like to store my data on disk...
1
by: Rucha | last post by:
We are using ACAServices in our project, and are passing entity classes as parameters to the ACAServiceMethod. We are using a private variable entityState to indicate whether the entity is...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.