Answered by:
Problem creating XmlWriter as UTF8?

Question
-
Hi,
I'm having trouble creating an xml document with UTF8 encoding. See this example:
var settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }; var xml = new StringBuilder(); using (var writer = XmlWriter.Create(xml, settings)) { // Here writer.Settings.Encoding == UTF16. Why??? }
As the example shows - I'm getting UTF16 regardless of the settings. Why? And how can I create an UTF8 Xml Writer?
--
WernerMonday, April 12, 2010 11:16 AM
Answers
-
You can set it, except when it isn't overridden by something else. If you're writing to a string, then the encoding will be overridden by UTF-16.
If you write to, for instance, a MemoryStream, then your encoding will take effect.
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects- Proposed as answer by Neha Garg - MSFTModerator Friday, May 28, 2010 5:57 PM
- Marked as answer by Werner Clausen Monday, May 31, 2010 6:57 AM
Friday, May 28, 2010 2:43 PMModerator
All replies
-
Strings in .NET are always Unicode, that is, UTF-16. There is no way to write any other encoding to a string.
If you write to a file, or to a stream, then you can get whatever encoding you like.
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects
- Marked as answer by Amadeo Casas - MSFTModerator Monday, April 19, 2010 6:11 PM
- Unmarked as answer by Werner Clausen Friday, May 28, 2010 6:13 AM
Monday, April 12, 2010 8:14 PMModerator -
Strings in .NET are always Unicode, that is, UTF-16. There is no way to write any other encoding to a string.
If you write to a file, or to a stream, then you can get whatever encoding you like.
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects
I dont understand your answer here - the problem is the XmlWriter settings. Why does it have a "Encoding" setter if I can't set it? It must be possible somehow to have the XmlWriter.Settings.Encoding == UTF8?Friday, May 28, 2010 6:13 AM -
You can set it, except when it isn't overridden by something else. If you're writing to a string, then the encoding will be overridden by UTF-16.
If you write to, for instance, a MemoryStream, then your encoding will take effect.
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects- Proposed as answer by Neha Garg - MSFTModerator Friday, May 28, 2010 5:57 PM
- Marked as answer by Werner Clausen Monday, May 31, 2010 6:57 AM
Friday, May 28, 2010 2:43 PMModerator -
You can set it, except when it isn't overridden by something else. If you're writing to a string, then the encoding will be overridden by UTF-16.
If you write to, for instance, a MemoryStream, then your encoding will take effect.
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects
Ok I get that now - it depends on the target. I have tried to write a file and the encoding is changed to utf8. Thanks!Monday, May 31, 2010 6:57 AM