M
Marco Herrn
Hi,
I need to serialize an object into a string representation to store it
into a database. So the SOAPFormatter seems to be the right formatter
for this purpose. Now I have the problem that this formatter writes into
a stream. And I am not used enough to C# to convert this to a string.
I tried the following code:
MemoryStream stream= new MemoryStream() ;
IFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, o);
byte[] buffer= stream.GetBuffer();
stream.Close();
UTF8Encoding encoding = new UTF8Encoding( );
string s= encoding.GetString(buffer);
stream= new MemoryStream();
encoding= new UTF8Encoding();
buffer= encoding.GetBytes(s);
stream.Write(buffer, 0, buffer.Length);
formatter = new SoapFormatter();
object o2= formatter.Deserialize(stream);
stream.Close();
That should serialize and deserialize the object. But I get the
exception "XmlException: The root element is missing." on
formatter.Deserialize(stream). Now I do not know what the problem here
is. When writing the string to a file it seems that there are
non-character bytes at the end of the string. I do not know whether this
really is the problem here, so I hope someone can point me to the right
direction.
Another problem is: Is UTF8 Encoding the correct one? Would be another
encoding more appropriate?
Regards
Marco
I need to serialize an object into a string representation to store it
into a database. So the SOAPFormatter seems to be the right formatter
for this purpose. Now I have the problem that this formatter writes into
a stream. And I am not used enough to C# to convert this to a string.
I tried the following code:
MemoryStream stream= new MemoryStream() ;
IFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, o);
byte[] buffer= stream.GetBuffer();
stream.Close();
UTF8Encoding encoding = new UTF8Encoding( );
string s= encoding.GetString(buffer);
stream= new MemoryStream();
encoding= new UTF8Encoding();
buffer= encoding.GetBytes(s);
stream.Write(buffer, 0, buffer.Length);
formatter = new SoapFormatter();
object o2= formatter.Deserialize(stream);
stream.Close();
That should serialize and deserialize the object. But I get the
exception "XmlException: The root element is missing." on
formatter.Deserialize(stream). Now I do not know what the problem here
is. When writing the string to a file it seems that there are
non-character bytes at the end of the string. I do not know whether this
really is the problem here, so I hope someone can point me to the right
direction.
Another problem is: Is UTF8 Encoding the correct one? Would be another
encoding more appropriate?
Regards
Marco