363,924 Members | 2598 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

xmlTextWriter to UTF-8

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


Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


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

"David" <david_tm3@hotmail.com> wrote in message
news:u3$Lh0B7FHA.3648@tk2msftngp13.phx.gbl...[color=blue]
> 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)
>
>[/color]


Nov 18 '05 #2

Jon Skeet [C# MVP]
P: n/a
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

Nov 18 '05 #3

Jon Skeet [C# MVP]
P: n/a
Jon Skeet [C# MVP]
Andrej Tozon wrote:[color=blue]
> 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.[/color]

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

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

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:1132309803.864097.142340@g44g2000cwa.googlegr oups.com...[color=blue]
> 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
>[/color]


Nov 18 '05 #5

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp