title: SQL 2005 XML Special/Escaped Characters from System.Xml.XmlDocument.OuterXml
body: <p>I have some XML that has creating using an XmlDocument object in C#.</p>
  
  <p>I am then inserting the xml data into an XML column in SQL 2005 using the XmlDocument.OuterXml method to get the raw xml.</p>
  
  <p>The problem I have is that the xml contains some special characters. Namely: <code>&amp;#x1F;&amp;#x1C;</code> This is because the xml is built up from user input from various sources.</p>
  
  <p>When I am performing the insert I get a SQL error message saying XML parsing: illegal xml character.</p>
  
  <pre><code>CREATE TABLE #MyTable (MyColumn XML)
  INSERT INTO #MyTable VALUES ('&lt;Element&gt;&amp;#x1F;&amp;#x1C;&lt;/Element&gt;')
  </code></pre>
  
  <p>Msg 9420, Level 16, State 1, Line 2</p>
  
  <p>XML parsing: line 1, character 15, illegal xml character</p>
  
  <p>Can someone point me in a direction as to how I can solve this?</p>
  
  <p>It will be extremely hard for me to change anything on the XmlDocument object or original xml itself, however, I can change the mechanism I use to get at the RAW XML. I can also change my database settings if required.</p>