423,516 Members | 2,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,516 IT Pros & Developers. It's quick & easy.

xmlTextWriter to UTF-8

P: n/a
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)
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"David" <da*******@hotmail.com> wrote in message
news:u3**************@tk2msftngp13.phx.gbl...
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)

Nov 18 '05 #2

P: n/a
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

Nov 18 '05 #3

P: n/a
Andrej Tozon wrote:
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

Nov 18 '05 #4

P: n/a
thanx, i'll try it))

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.