StringWriter writes to strings, whose underlying memory
representation is always UTF-16 in the CLR (let's not
get into the surrogate pair issue here...).
Thus, if you have StringWriter write to a string, it
*will* be UTF-16 whether the first line in the string
claims so or not. This is also how MSXML behaves, by the
way.
Of course, when you take the string and convert it into
a memory buffer in another encoding you need to take
care of converting the encoding-providing header, which
means extra work to you. Whether this is actually a good
design or not is another issue...
I would expect the fact that System.String is unicode
has something to do with it :) So, it just won't support
another format.
If you want another encoding you should probably use
something along the lines of a binary stream.
An UTF8 encoded xml document stored in a UTF16 string
doesn't really make much sence now does it :)
Just my 2c!
Thanks for all the input guys. Like I said, It seems to
just reaffirm what I wrote - You can't make you XML
document's header UTF-8 encoded without going through
some hoops.
Perhaps this is something that would best be solved
using a method of the XMLTextWriter, something like
'SetEncodingHeader(Encoding encoding)' that would allow
this without too much hassle..
"I would expect the fact that System.String is
unicode has something to do with it :)"
Technically, UTF-8 is also Unicode--just a different
encoding for it ;).
I know that UTF-8 is unicode as well.
It's just that in the BCL UTF-16 is referred to as
Encoding.Unicode... which is why I wrote that statement.
Anyways, the System.String class is an UTF16 string, so
an XmlTextWriter with SetEncodingHeader would only
enable you to have an encoding mismatch...
Morty :)
First of all thanks a lot for this piece of code, as its
just what I was looking for.
Unfortunately I am able to get only 4096 bytes of data
from the MemoryBuffer object.