469,280 Members | 1,998 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

xmlTextWriter to UTF-8

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
4 17607
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
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
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
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.

Similar topics

5 posts views Thread by Jain, Pranay Kumar | last post: by
2 posts views Thread by Jesper Stocholm | last post: by
3 posts views Thread by Magnus | last post: by
2 posts views Thread by Josh Newman | last post: by
5 posts views Thread by Gilgamesh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.