XML Encoding

V

viepia

Hi,

In program 1 XmlWriter.Create(string pathname, XmlWriterSettings
settings), its encoding is UTF-8 <?xml version="1.0"
encoding="utf-8"?>, in program 2 XmlWriter.Create(StringWriter stw,
XmlWriterSettings settings), it encoding is UTF-16 <?xml version="1.0"
encoding="utf-16"?>
. The settings are identical in each program. The XML text is
saved as 8 bit ASCII in each program, so "utf-16" is wrong in Program
2. What can I do to get Program 2 to make the correct "utf-8"
header?

Thanks,
Viepia
 
J

Jon Skeet [C# MVP]

In program 1 XmlWriter.Create(string pathname, XmlWriterSettings
settings), its encoding is UTF-8 <?xml version="1.0"
encoding="utf-8"?>, in program 2 XmlWriter.Create(StringWriter stw,
XmlWriterSettings settings), it encoding is UTF-16 <?xml version="1.0"
encoding="utf-16"?>
. The settings are identical in each program. The XML text is
saved as 8 bit ASCII in each program, so "utf-16" is wrong in Program
2.

Well, firstly there's no such thing as 8 bit ASCII. The first version
is saving it as UTF-8.

Secondly, the second version is only saving it to a StringWriter. If
you choose to then save that string to disk as UTF-8 instead of UTF-16,
that's your choice rather than .NET getting it wrong on your behalf.
However...
What can I do to get Program 2 to make the correct "utf-8"
header?

You want a StringWriter which returns UTF-8 as the encoding. I have a
StringWriterWithEncoding class in my MiscUtil library:

http://pobox.com/~skeet/csharp/miscutil
 

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