End of Stream encountered before parsing was completed?

Mister Dev picture Mister Dev · Nov 20, 2008 · Viewed 49.4k times · Source

I am trying to deserialize a stream but I always get this error "End of Stream encountered before parsing was completed"?

Here is the code:

        //Some code here
        BinaryFormatter b = new BinaryFormatter();
        return (myObject)b.Deserialize(s);//s---> is a Stream object that has been fill up with data some line over here

Any one have ideas?

Answer

Patrick Desjardins picture Patrick Desjardins · Nov 20, 2008

Try to set the position to 0 of your stream and do not use your object but the object type.

        BinaryFormatter b = new BinaryFormatter();
        s.Position = 0;
        return (YourObjectType)b.Deserialize(s);