deserialize and utf8 encoding

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);
}
}
 
C

Christof Nordiek

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top