xmlTextWriter to UTF-8

D

David

hello... i'm using this declarations:

StringWriter w = new StringWriter();

XmlTextWriter xml = new XmlTextWriter(w);

how can i make the output xml (in string) to be writen in UTF-8 encoding?
(it writen in utf-16 now)

..net 1.1 (VS2003)
 
A

Andrej Tozon

Hi David,
Why do you need UTF-8 encoded string?

In-memory strings are unicode (UTF-16); I don't know any other way to make
them UTF-8 without writing them to file or a memory stream.

Andrej
 
J

Jon Skeet [C# MVP]

You need to derive from StringWriter, like this:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}

public override Encoding Encoding
{
get { return encoding; }
}
}

Then instead of creating a StringWriter, create a
StringWriterWithEncoding, specifying the encoding you want
(Encoding.UTF8 in this case).

Jon
 
J

Jon Skeet [C# MVP]

Andrej said:
Why do you need UTF-8 encoded string?

In-memory strings are unicode (UTF-16); I don't know any other way to make
them UTF-8 without writing them to file or a memory stream.

Until the XML is written out, it's irrelevant - but if you want to
write the result out in UTF-8 encoded format, it's a bit of a pain if
the XML declaration specifies that it's in UTF-16 (which it will by
default with StringWriter).

Jon
 

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