.comment-link {margin-left:.6em;}

Ben Cops

Tuesday, August 24, 2004

XMLSerializer encoding

The XMLSerializer spits out streams encoded as UTF-8 by default, if you just feed it a bare stream to serialise to. Force it to use a more BTS friendly encoding by providing it with a StreamWriter or similar with a explicit encoding;

/// Serialises an object to a unicode string

/// The object to serialise
/// A string representing the serialised object
public static string Serialise(object objectToSerialise)
//create an xml serialiser for the object's type
XmlSerializer ser = new XmlSerializer(objectToSerialise.GetType());
//create a memory stream object to hold the data for the serialised type
MemoryStream baseStream = new MemoryStream();
//create a streamwriter, so we can write in unicode encoding to the stream
StreamWriter encodingWriter = new StreamWriter(baseStream, System.Text.Encoding.Unicode);
//get xmlserialiser to write to the base memorystream through the encoded stream writer
ser.Serialize(encodingWriter, objectToSerialise);
//set the memorystream back to the start, and extract the unicode string from it
baseStream.Position = 0;
return CommonServices.StreamToString(baseStream);



  • Hey bud
    Hi there again
    Remember me?
    Here is a clue....

    Hope to hear from ya. :)

    By Anonymous RJ, at 2:30 pm  

Post a Comment

<< Home