deserialize and utf8 encoding

  • Thread starter Thread starter Heron
  • Start date Start date
H

Heron

Hi,

I'm having a problem deserializing my streams since they are utf8 encoded
(they are being received over tcp/ip) so I was looking for a way to make the
serializer use utf8, is there any?

I'm getting the following error:
There is an error in XML document (0, 0).
innerexception: There is no Unicode byte order mark. Cannot switch to
Unicode."

public T Deserialize(Stream s)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
try
{
s.Position = 0;
return (T)serializer.Deserialize(s);
}
catch (Exception ex)
{
throw new InvalidOperationException("Failed to " +
"create object", ex);
}
}
 
The Deserialize method has an overload with a TextReader parameter. You
could try to encapsulate the Stream with a StreamReader and call the
deserialize with this as argument. Smothin like:

StreamReader sr = new StreamReader(s, Encoding.UTF8);
return (T)serializer.Deserialize(sr);

hth
 
Back
Top