By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

How do I set the utf

P: n/a
The following is what I have :

XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Indent = true;
xmlSettings.IndentChars = " ";
xmlSettings.Encoding = System.Text.Encoding.UTF8;
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb, xmlSettings))

============
The problem I have is the xml comes out with
<?xml Version="1.0" encoding="utf-16"?>

It really does not matter what I have in the xmlSettings, it always comes
out utf-16.

???? What am i missing ?????

Thanks.


Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Phil Hunt" <aa*@aaa.comwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
The following is what I have :

XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Indent = true;
xmlSettings.IndentChars = " ";
xmlSettings.Encoding = System.Text.Encoding.UTF8;
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb, xmlSettings))

============
The problem I have is the xml comes out with
<?xml Version="1.0" encoding="utf-16"?>

It really does not matter what I have in the xmlSettings, it always comes
out utf-16.
You are writing to a string builder. All strings in .NET are unicode
(UTF-16) so it doesn't matter what encoding you set on settings because
there isn't any encoding to be done.

If you were writing to a File or some other stream it would work.

--
Anthony Jones - MVP ASP/ASP.NET
Jun 27 '08 #2

P: n/a
On Jun 26, 3:49 pm, "Phil Hunt" <a...@aaa.comwrote:
The following is what I have :

XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Indent = true;
xmlSettings.IndentChars = " ";
xmlSettings.Encoding = System.Text.Encoding.UTF8;
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb, xmlSettings))

============
The problem I have is the xml comes out with
<?xml Version="1.0" encoding="utf-16"?>

It really does not matter what I have in the xmlSettings, it always comes
out utf-16.

???? What am i missing ?????
You need a StringWriterWithEncoding, rather than passing in a
StringBuilder directly.

The StringWriterWithEncoding code is really simple though:

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

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

You could add other constructor overloads should you wish, of course.

Jon
Jun 27 '08 #3

P: n/a
Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:f0**********************************@x41g2000 hsb.googlegroups.com...
On Jun 26, 3:49 pm, "Phil Hunt" <a...@aaa.comwrote:
>The following is what I have :

XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Indent = true;
xmlSettings.IndentChars = " ";
xmlSettings.Encoding = System.Text.Encoding.UTF8;
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb, xmlSettings))

============
The problem I have is the xml comes out with
<?xml Version="1.0" encoding="utf-16"?>

It really does not matter what I have in the xmlSettings, it always comes
out utf-16.

???? What am i missing ?????

You need a StringWriterWithEncoding, rather than passing in a
StringBuilder directly.

The StringWriterWithEncoding code is really simple though:

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

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

You could add other constructor overloads should you wish, of course.

Jon

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.