title: How to save XML without characters being escaped?
body: <p>In my C# app, XML data may contain arbitrary element text that's already been pre-processed, so that (among other things) illegal characters have been converted to their escaped (xml character entity encoded) form.</p>
  
  <p>Example:  <code>&lt;myElement&gt;this &amp; that&lt;/myElement&gt;</code> has been converted to <code>&lt;myElement&gt;this &amp;amp; that&lt;/myElement&gt;</code>.</p>
  
  <p>The problem is that when I use XmlTextWriter to save the file, the <strong>'&amp;'</strong> is getting re-escaped into <code>&lt;myElement&gt;this &amp;amp;amp; that&lt;/myElement&gt;</code>.  I don't want that extra <strong>&amp;amp;</strong> in the string.</p>
  
  <p>Another example: <code>&lt;myElement&gt;• bullet&lt;/myElement&gt;</code>, my processing changes it to <code>&lt;myElement&gt;&amp;#8226; bullet&lt;/myElement&gt;</code> which gets saved to <code>&lt;myElement&gt;&amp;amp;#8226; bullet&lt;/myElement&gt;</code>.  All I want output to the file is the <code>&lt;myElement&gt;&amp;#8226; bullet&lt;/myElement&gt;</code> form.</p>
  
  <p>I've tried various options on the various XmlWriters, etc but can't seem to get the raw strings to get output correctly.  And why can't the XML parser recognize &amp; not rewrite already a valid escapes?</p>
  
  <p><strong>update:</strong> afer more debugging, I found that element text strings (actually all strings including element tags, names, attributes, etc. ) get encoded whenever they get copied into the .net xml object data (CDATA being an exception) by an internal class called XmlCharType under System.Xml.  So the problem has nothing to do with the XmlWriters.  It looks like the best way to solve the problem is to un-escape the data when it's output, either by using something like:</p>
  
  <pre><code>string output = System.Net.WebUtility.HtmlDecode(xmlDoc.OuterXml);
  </code></pre>
  
  <p>Which will probably evolve into a custom XmlWriter in order to preserve formatting, etc.</p>
  
  <p>Thanks all for the helpful suggestions.</p>