I’m currently serializing an object using XMLSerializer, and the resulting XML starts with:
<?xml version="1.0" encoding="utf-16"?>
Which I would like to get rid of, because in this particular case I don’t need it (I’ll only use the serialized string to deserialize later with my own code, so I can re-add it later when needed).
I’m also trying to do this as fast as possible, since we’ll be doing TONS of these serializations.
So the question is, can I count on this signature to always be exactly the same? (As in, can I just remove the first 39 characters of the resulting string, and then add back that exact same string when deserializing?)
Or can something make the encoding be different, for example?
Thanks
The answer to your question is in the code you didn’t show us – how you did the serialization. You probably serialized to a StringWriter, or directly to a StringBuilder. Strings in .NET are UTF-16. If you serialize to a string, you have no choice but to get UTF-16 encoding.
In other situations, the encoding is dictated by the destination. If you serialize to a TextWriter of some sort, then the encoding of the TextWriter will be used, unless overridden. If you serialize to an XmlWriter, then the XmlWriterSettings will determine the encoding used.
I recommend that you leave the signature alone, unless you’re an expert in XML. The .NET XML APIs understand the rules of XML. Unless you understand them just as well, I recommend you leave it to the expert.