How do I set the utf

P

Phil Hunt

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.
 
A

Anthony Jones

Phil Hunt said:
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.
 
J

Jon Skeet [C# MVP]

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
 
P

Phil Hunt

Thanks.



Jon Skeet said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top