<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Tip: Force UTF8 or other encoding for XmlWriter with StringBuilder</title>
	<atom:link href="http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/</link>
	<description>secrets and mysteries from the realms of the technocrats and other such ramblings</description>
	<lastBuildDate>Wed, 01 Feb 2012 04:10:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Abel Braaksma</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-50217</link>
		<dc:creator>Abel Braaksma</dc:creator>
		<pubDate>Wed, 26 Oct 2011 16:49:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-50217</guid>
		<description>That&#039;s an interesting link. Essentially it explains how to create your own Encoding class and use that instead, plus that you need to use a MemoryStream as opposed to a StringWriter. It&#039;s still quite a lot of work, but nice to know that there are other workarounds.</description>
		<content:encoded><![CDATA[<p>That&#8217;s an interesting link. Essentially it explains how to create your own Encoding class and use that instead, plus that you need to use a MemoryStream as opposed to a StringWriter. It&#8217;s still quite a lot of work, but nice to know that there are other workarounds.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matheus Rufca</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-48970</link>
		<dc:creator>Matheus Rufca</dc:creator>
		<pubDate>Tue, 18 Oct 2011 14:31:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-48970</guid>
		<description>I found this and it works:
MemoryStream memoryStream = new MemoryStream();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UTF8Encoding(false);
xmlWriterSettings.ConformanceLevel = ConformanceLevel.Document;
xmlWriterSettings.Indent = true;
 
XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement(&quot;root&quot;, &quot;http://www.timvw.be/ns&quot;);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
 
string xmlString = Encoding.UTF8.GetString(memoryStream.ToArray());


font: http://www.timvw.be/2007/01/08/generating-utf-8-with-systemxmlxmlwriter/</description>
		<content:encoded><![CDATA[<p>I found this and it works:<br />
MemoryStream memoryStream = new MemoryStream();<br />
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();<br />
xmlWriterSettings.Encoding = new UTF8Encoding(false);<br />
xmlWriterSettings.ConformanceLevel = ConformanceLevel.Document;<br />
xmlWriterSettings.Indent = true;</p>
<p>XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);<br />
xmlWriter.WriteStartDocument();<br />
xmlWriter.WriteStartElement(&#8220;root&#8221;, &#8220;http://www.timvw.be/ns&#8221;);<br />
xmlWriter.WriteEndElement();<br />
xmlWriter.WriteEndDocument();<br />
xmlWriter.Flush();<br />
xmlWriter.Close();</p>
<p>string xmlString = Encoding.UTF8.GetString(memoryStream.ToArray());</p>
<p>font: <a href="http://www.timvw.be/2007/01/08/generating-utf-8-with-systemxmlxmlwriter/" rel="nofollow">http://www.timvw.be/2007/01/08/generating-utf-8-with-systemxmlxmlwriter/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lowes Ceiling Fans</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-42251</link>
		<dc:creator>Lowes Ceiling Fans</dc:creator>
		<pubDate>Fri, 16 Sep 2011 16:32:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-42251</guid>
		<description>Thanks  for every other informative web site. The place else may I am getting that type of information written in such an ideal approach? I&#039;ve a challenge that I&#039;m just now working on, and I&#039;ve been at the glance out for such info.</description>
		<content:encoded><![CDATA[<p>Thanks  for every other informative web site. The place else may I am getting that type of information written in such an ideal approach? I&#8217;ve a challenge that I&#8217;m just now working on, and I&#8217;ve been at the glance out for such info.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Uttam</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-41420</link>
		<dc:creator>Uttam</dc:creator>
		<pubDate>Fri, 09 Sep 2011 12:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-41420</guid>
		<description>Try This ---&gt;

 public class StringWriterWithEncoding : StringWriter
    {
        private readonly Encoding encoding;
        public StringWriterWithEncoding(StringBuilder sb, Encoding newEncoding) : base(sb) { encoding = newEncoding; }
        public override Encoding Encoding { get { return encoding ?? base.Encoding; } }
    }


 private static string GenerateXML()
        {
            var stringBuilder = new StringBuilder();
            var xmlTextWriter = new XmlTextWriter(new StringWriterWithEncoding(stringBuilder, Encoding.UTF8)) { Formatting = Formatting.None };
            xmlTextWriter.WriteStartDocument();

	    #region Generatere your xml
      	    #endregion

        
            xmlTextWriter.WriteEndDocument();
            xmlTextWriter.Flush();
            xmlTextWriter.Close();
            return stringBuilder.ToString();
        }</description>
		<content:encoded><![CDATA[<p>Try This &#8212;&gt;</p>
<p> public class StringWriterWithEncoding : StringWriter<br />
    {<br />
        private readonly Encoding encoding;<br />
        public StringWriterWithEncoding(StringBuilder sb, Encoding newEncoding) : base(sb) { encoding = newEncoding; }<br />
        public override Encoding Encoding { get { return encoding ?? base.Encoding; } }<br />
    }</p>
<p> private static string GenerateXML()<br />
        {<br />
            var stringBuilder = new StringBuilder();<br />
            var xmlTextWriter = new XmlTextWriter(new StringWriterWithEncoding(stringBuilder, Encoding.UTF8)) { Formatting = Formatting.None };<br />
            xmlTextWriter.WriteStartDocument();</p>
<p>	    #region Generatere your xml<br />
      	    #endregion</p>
<p>            xmlTextWriter.WriteEndDocument();<br />
            xmlTextWriter.Flush();<br />
            xmlTextWriter.Close();<br />
            return stringBuilder.ToString();<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abel Braaksma</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-6406</link>
		<dc:creator>Abel Braaksma</dc:creator>
		<pubDate>Wed, 14 Apr 2010 00:31:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-6406</guid>
		<description>Hi Graig, just have the guy read the very first sentence of the subject on strings in the .NET Framework reference. This is true for all .NET languages that are CLR compliant (like VB):

&lt;em&gt;&quot;A string is an object of type String whose value is text. Internally, the text is stored as a readonly collection of Char objects, each of which represents one Unicode character encoded in UTF-16.&quot;&lt;/em&gt; (See this &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms228362.aspx&quot; rel=&quot;nofollow&quot;&gt;reference&lt;/a&gt;)

(note that this was true from the very first version of .NET. In fact, they all follow the Unicode version 3.0 standard pretty flawlessly internally. Unfortunately, Microsoft never upgraded, not even for the recent VS 2010)</description>
		<content:encoded><![CDATA[<p>Hi Graig, just have the guy read the very first sentence of the subject on strings in the .NET Framework reference. This is true for all .NET languages that are CLR compliant (like VB):</p>
<p><em>&#8220;A string is an object of type String whose value is text. Internally, the text is stored as a readonly collection of Char objects, each of which represents one Unicode character encoded in UTF-16.&#8221;</em> (See this <a href="http://msdn.microsoft.com/en-us/library/ms228362.aspx" rel="nofollow">reference</a>)</p>
<p>(note that this was true from the very first version of .NET. In fact, they all follow the Unicode version 3.0 standard pretty flawlessly internally. Unfortunately, Microsoft never upgraded, not even for the recent VS 2010)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-3851</link>
		<dc:creator>Craig</dc:creator>
		<pubDate>Wed, 06 Jan 2010 15:58:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-3851</guid>
		<description>This author disagrees with you, and says that the default encoding in VB.net is UTF-8. What is your opinion?

http://techrepublic.com.com/5208-6230-0.html?forumID=102&amp;threadID=226398&amp;messageID=3222797</description>
		<content:encoded><![CDATA[<p>This author disagrees with you, and says that the default encoding in VB.net is UTF-8. What is your opinion?</p>
<p><a href="http://techrepublic.com.com/5208-6230-0.html?forumID=102&#038;threadID=226398&#038;messageID=3222797" rel="nofollow">http://techrepublic.com.com/5208-6230-0.html?forumID=102&#038;threadID=226398&#038;messageID=3222797</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abel Braaksma</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-6408</link>
		<dc:creator>Abel Braaksma</dc:creator>
		<pubDate>Sat, 02 Jan 2010 06:40:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-6408</guid>
		<description>That was what this little article was about, to explain the particular reasons and solutions why UTF-16 is used even when you don&#039;t want that. There are other ways. For instance, the header is correctly honored if you simply write your output to a file (but watch the method you use, you must use an XML classes method)</description>
		<content:encoded><![CDATA[<p>That was what this little article was about, to explain the particular reasons and solutions why UTF-16 is used even when you don&#8217;t want that. There are other ways. For instance, the header is correctly honored if you simply write your output to a file (but watch the method you use, you must use an XML classes method)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-3707</link>
		<dc:creator>Gary</dc:creator>
		<pubDate>Thu, 31 Dec 2009 18:58:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-3707</guid>
		<description>Nice explanation on the utf-16 vs. utf-8 XML header issue. I have a program that is invoked from the command line. Its output, normally sent to the console, can be redirected to a file. Problem is, that&#039;s an 8-bit text file, even if the content is XML with a utf-16 header. The result is that the XML file can&#039;t be opened in a browser because the XML header does not match the byte size of the text in the file. So, I took the simple route of string replacement, but I had wondered why I couldn&#039;t just provide UTF8 settings to the XmlWriter constructor.</description>
		<content:encoded><![CDATA[<p>Nice explanation on the utf-16 vs. utf-8 XML header issue. I have a program that is invoked from the command line. Its output, normally sent to the console, can be redirected to a file. Problem is, that&#8217;s an 8-bit text file, even if the content is XML with a utf-16 header. The result is that the XML file can&#8217;t be opened in a browser because the XML header does not match the byte size of the text in the file. So, I took the simple route of string replacement, but I had wondered why I couldn&#8217;t just provide UTF8 settings to the XmlWriter constructor.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abel Braaksma</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-771</link>
		<dc:creator>Abel Braaksma</dc:creator>
		<pubDate>Thu, 24 Sep 2009 01:08:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-771</guid>
		<description>I see your point, and I like this other approach, however, it still does not allow you to simply write with any encoding string to a StringBuilder or TextWriter. One idea to use StringBuilder and XmlWriter is efficiency, by writing it first to a memory stream, then converting it to a byte array (second copy) and then converting it to a string (third copy) defies that idea a bit.

At the other end, when you need string output (i.e. for display purposes), it is debatable whether such optimizations are needed.

Though I can agree that your approach is the obvious more &quot;correct&quot; way, and the OmitXmlDeclaration is yet another &quot;dirty&quot; way because you have to prepend it by hand, which you just want to prevent by using a standardized writer, I don&#039;t see an easy way of implementing this more correct way as a simple accessible extension so that users do not need to worry about this issue.

I don&#039;t see it as forcing something it is not, because a string is always a UTF-16 string internally and here it should represent a data stream as a string. I don&#039;t force the string to be something else, the string will remain UTF-16, but will output with the intended encoding just like any other XmlWriter.

-- Abel --</description>
		<content:encoded><![CDATA[<p>I see your point, and I like this other approach, however, it still does not allow you to simply write with any encoding string to a StringBuilder or TextWriter. One idea to use StringBuilder and XmlWriter is efficiency, by writing it first to a memory stream, then converting it to a byte array (second copy) and then converting it to a string (third copy) defies that idea a bit.</p>
<p>At the other end, when you need string output (i.e. for display purposes), it is debatable whether such optimizations are needed.</p>
<p>Though I can agree that your approach is the obvious more &#8220;correct&#8221; way, and the OmitXmlDeclaration is yet another &#8220;dirty&#8221; way because you have to prepend it by hand, which you just want to prevent by using a standardized writer, I don&#8217;t see an easy way of implementing this more correct way as a simple accessible extension so that users do not need to worry about this issue.</p>
<p>I don&#8217;t see it as forcing something it is not, because a string is always a UTF-16 string internally and here it should represent a data stream as a string. I don&#8217;t force the string to be something else, the string will remain UTF-16, but will output with the intended encoding just like any other XmlWriter.</p>
<p>&#8211; Abel &#8211;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.undermyhat.org/blog/2009/08/tip-force-utf8-or-other-encoding-for-xmlwriter-with-stringbuilder/comment-page-1/#comment-762</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Wed, 23 Sep 2009 11:02:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.undermyhat.org/blog/?p=315#comment-762</guid>
		<description>Thanks for the article. It does shine some light on this topic.

An easier and more &quot;correct&quot; way would be:

using (MemoryStream buffer = new MemoryStream())
{
    using (StreamWriter stream = new StreamWriter(buffer, Encoding.UTF8))
    {
        using (XmlWriter writer = XmlWriter.Create(stream))
        {
            writer.WriteStartElement(&quot;should-be utf-8&quot;);
            writer.WriteEndElement();
        }
    }
    byte[] xmlData = buffer.ToArray();
}

// For testing or to put into a textbox:
string xml = Encoding.UTF8.GetString(xmlData);  // You might want to strip the BOM here

This method does create the underlying data in the correct encoding and it does write the BOM.

Why force something (StringBuilder) to UTF-8 if it is not? If using StringBuilder you can create the XML with XmlWriter and the setting OmitXmlDeclaration and then prepend the XML declaration yourself when converting the StringBuilder string to UTF-8 or whatever encoding.</description>
		<content:encoded><![CDATA[<p>Thanks for the article. It does shine some light on this topic.</p>
<p>An easier and more &#8220;correct&#8221; way would be:</p>
<p>using (MemoryStream buffer = new MemoryStream())<br />
{<br />
    using (StreamWriter stream = new StreamWriter(buffer, Encoding.UTF8))<br />
    {<br />
        using (XmlWriter writer = XmlWriter.Create(stream))<br />
        {<br />
            writer.WriteStartElement(&#8220;should-be utf-8&#8243;);<br />
            writer.WriteEndElement();<br />
        }<br />
    }<br />
    byte[] xmlData = buffer.ToArray();<br />
}</p>
<p>// For testing or to put into a textbox:<br />
string xml = Encoding.UTF8.GetString(xmlData);  // You might want to strip the BOM here</p>
<p>This method does create the underlying data in the correct encoding and it does write the BOM.</p>
<p>Why force something (StringBuilder) to UTF-8 if it is not? If using StringBuilder you can create the XML with XmlWriter and the setting OmitXmlDeclaration and then prepend the XML declaration yourself when converting the StringBuilder string to UTF-8 or whatever encoding.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)

Served from: www.undermyhat.org @ 2012-02-08 04:03:44 -->
