I have a file which has unicode of a byte array which is a serialized object. I tried reading that file using the code below.
string unicodeString = File.ReadAllText(filename);
This works fine, when i tried to get that Byte array back, for deserialization.
What I am looking for is, read only the particular lines of a file and try to convert that unicode string to bytes. For that I tried.
string unicodeString = string.join("", File.ReadAllLines(filename).Take(4).ToArray());
Here I used 4 because, the file has 4 lines of unicode string
byte[] _bytes = System.Text.Encoding.Unicode.GetBytes(unicodeString );
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (var ms = new MemoryStream(_bytes))
{
myobject data = (myobject)bformatter.Deserialize(ms);
}
I can able to get the string but unable to deserialize. My objective is to I will many such objects in the file and I'll retrieve only those lines and deserialize that to object.
It throws the following exception.
{"Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization."}