Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

serialization and deserialization problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AnkitAsDeveloper [Ankit] - 25 Oct 2005 15:10 GMT
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.SerializationException' 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/
AnkitAsDeveloper [Ankit] - 25 Oct 2005 15:24 GMT
I am working in Managed C++ .NET Framework RC1
Willy Denoyette [MVP] - 25 Oct 2005 20:06 GMT
DateTime is a value type, remove the ^  from...

DateTime Arrival;
DateTime Departure;

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

Willy.

> Hi i am serializing a 'ref struct' object as follows :
>
[quoted text clipped - 94 lines]
> [Ankit Jain]
> http://www.ankitjain.info/ankit/
AnkitAsDeveloper [Ankit] - 26 Oct 2005 13:05 GMT
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/

> DateTime is a value type, remove the ^  from...
>
[quoted text clipped - 104 lines]
> > [Ankit Jain]
> > http://www.ankitjain.info/ankit/

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.