views:

20

answers:

1

From MSDN:

There are two reasons to implement this interface. The first is to control how your object is serialized or deserialized by the XmlSerializer. For example, you can chunk data into bytes instead of buffering large data sets, and also avoid the inflation that occurs when the data is encoded using Base64 encoding.

How does implementing IXmlSerializable let me avoid base64's size inflation?

I mean, imagine I've implemented IXmlSerializable and I want to write a byte-array to the XmlWriter. The least inflating way to do that is XmlWriter.WriteBase64, as far as I know - since I cannot write the data directly - it would contain bytes invalid in XML.

+1  A: 

Yes, base64 is not very efficient. The assumption here is that you could encode the byte[] yourself in a different way and save space. Maybe a run-length encoding. Maybe you can gzip the array and base64 encode the compressed data. Something like that.

Hans Passant
Right. A runlength encoding was what I was planning to do anyway. But from the wording in MSDN, it seemed they mean there's some direct way to write the bytes without base64-encoding them. For example some method like XmlWriter.WriteBytesUnencoded. Because they say " *avoid* the inflation", and your way is more like "deflate, then inflate back a bit".
Stefan Monov