Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

How do I set the utf

Question posted by: Phil Hunt (Guest) on June 27th, 2008 07:20 PM
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.






Anthony Jones's Avatar
Anthony Jones
Guest
n/a Posts
June 27th, 2008
07:20 PM
#2

Re: How do I set the utf
"Phil Hunt" <aaa@aaa.comwrote in message
news:uXiDRv51IHA.5048@TK2MSFTNGP06.phx.gbl...
Quote:
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



Jon Skeet [C# MVP]'s Avatar
Jon Skeet [C# MVP]
Guest
n/a Posts
June 27th, 2008
07:20 PM
#3

Re: How do I set the utf
On Jun 26, 3:49 pm, "Phil Hunt" <a...@aaa.comwrote:
Quote:
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

Phil Hunt's Avatar
Phil Hunt
Guest
n/a Posts
June 27th, 2008
07:20 PM
#4

Re: How do I set the utf
Thanks.



"Jon Skeet [C# MVP]" <skeet@pobox.comwrote in message
news:f0e648fa-6ca6-4cb3-8017-68802d7413c2@x41g2000hsb.googlegroups.com...
Quote:
On Jun 26, 3:49 pm, "Phil Hunt" <a...@aaa.comwrote:
Quote:
>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




 
Not the answer you were looking for? Post your question . . .
190,472 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors