Let's look at msdn. The most common examples of Object serialization are XML serialization in file streams. Because of the development needs, I must serialize objects in binary mode in the internal stream, the original code is as follows: public sealed class serializer
{
Private serializer (){}
Public static string serializeobject (Object OBJ)
{
Iformatter formatter = new binaryformatter ();
String result = string. empty;
Using (memorystream stream = new memorystream ())
{
Formatter. serialize (stream, OBJ );
Byte [] BYT = new byte [stream. Length];
Byt = stream. toarray ();
Result = encoding. utf8.getstring (BYT, 0, byt. Length );
Stream. Flush ();
}
Return result;
}
Public static object deserializeobject (string Str)
{
Iformatter formatter = new binaryformatter ();
Byte [] BYT = encoding. utf8.getbytes (STR );
Object OBJ = NULL;
Using (Stream stream = new memorystream (BYT, 0, byt. Length ))
{
OBJ = formatter. deserialize (Stream );
}
Return OBJ;
}
}
The error message is as follows: serializertest. serializeobjecttest causes an exception:
System. runtime. serialization. serializationexception: the binary stream "0" does not contain valid binaryheader. This may be due to invalid streams or object version changes between serialization and deserialization.
After debugging, we found that in the serializeobject method, "result = encoding. utf8.getstring (BYT, 0, byt. length); "after this line of code is run, the result value is \ 0 \ 0 ...... "It's no wonder that an error will be reported at the beginning of a string.
After that, I changed binaryformatter to xmlserializer and the other ones will not change, but the result will be returned normally, but that is not what I want, the key to the problem is to convert the byte array to the string. Therefore, I will use gb2312, ANSI, Unicode, and Other encoding to convert the string. Then I will find a method in the convert class, the modified code is as follows:
Public sealed class serializer
{
Private serializer (){}
Public static string serializeobject (Object OBJ)
{
Iformatter formatter = new binaryformatter ();
String result = string. empty;
Using (memorystream stream = new memorystream ())
{
Formatter. serialize (stream, OBJ );
Byte [] BYT = new byte [stream. Length];
Byt = stream. toarray ();
// Result = encoding. utf8.getstring (BYT, 0, byt. Length );
Result = convert. tobase64string (BYT );
Stream. Flush ();
}
Return result;
}
Public static object deserializeobject (string Str)
{
Iformatter formatter = new binaryformatter ();
// Byte [] BYT = encoding. utf8.getbytes (STR );
Byte [] BYT = convert. frombase64string (STR );
Object OBJ = NULL;
Using (Stream stream = new memorystream (BYT, 0, byt. Length ))
{
OBJ = formatter. deserialize (Stream );
}
Return OBJ;
}
}
Once the unit test is passed again, the modification is valid. Debug to "result = convert. tobase64string (BYT); ", the value of the result does not start with" \ 0 ". It is a letter, which proves the serialization success. After that, we should pay more attention to 64-Bit String Conversion, which often brings surprising results. This case is finally closed.