Connecting Tech Pros Worldwide Forums | Help | Site Map

xmlTextWriter to UTF-8

David
Guest
 
Posts: n/a
#1: Nov 18 '05
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)



Andrej Tozon
Guest
 
Posts: n/a
#2: Nov 18 '05

re: xmlTextWriter to UTF-8


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]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 18 '05

re: xmlTextWriter to UTF-8


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

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Nov 18 '05

re: xmlTextWriter to UTF-8


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

David
Guest
 
Posts: n/a
#5: Nov 18 '05

re: xmlTextWriter to UTF-8


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]


Closed Thread