title: XMLSerializer - issue with UTF-8 vs UTF-16 Code
body: <p>I am trying to serialize a simple object (5 string properties) into XML to save to a DB Image field.  Then I need to DeSerialize it back into a string later in the program.<br>
  However, I am getting some errors - caused by the XML being saved thinking it is in UTF-16 - however, when I load it from the DB back into a string - it thinks it is a UTF 8 String. </p>
  
  <p>The error I get is </p>
  
  <ul>
  <li>InnerException  {"There is no Unicode byte order mark. Cannot switch to Unicode."}  System.Exception {System.Xml.XmlException}
  -- Message  "There is an error in XML document (0, 0)." string</li>
  </ul>
  
  <p>Is this happening because of the two different ways I save and load the string to/from the DB?  On the save I am using a StringBuilder - but on the load from  DB I  am using just a String.</p>
  
  <p>Thoughts?  </p>
  
  <p>Serialize and Save to DB</p>
  
  <pre><code>        // Now Save the OBject XML to the Query Tables
          var serializer = new XmlSerializer(ExportConfig.GetType());
          StringBuilder StringResult = new StringBuilder();
          using (var writer = XmlWriter.Create(StringResult))
          {
             serializer.Serialize(writer, ExportConfig);
          }
          //MessageBox.Show("XML :  " + StringResult);
          // Now Save to the Query
  
      try
      {
          string UpdateSQL = "Update ZQryRpt "
                       + "  Set ExportConfig = " + TAGlobal.QuotedStr(StringResult.ToString())
                       + "  where QryId = " + TAGlobal.QuotedStr(((DataRowView)bindingSource_zQryRpt.Current).Row["QryID"].ToString())
                       ;
          ExecNonSelectSQL(UpdateSQL, uniConnection_Config);
      }
      catch (Exception Error)
      {
          MessageBox.Show("Error Setting ExportConfig: " + Error.Message);
      }
  </code></pre>
  
  <p>Load from DB And Deserialize </p>
  
  <pre><code>    byte[] binaryData = (byte[])((DataRowView)bindingSource_zQryRpt.Current).Row["ExportConfig"];
      string XMLStored = System.Text.Encoding.UTF8.GetString(binaryData, 0, binaryData.Length);
  
      if (XMLStored.Length &gt; 0)
      {
          IIDExportObject ExportConfig = new IIDExportObject();
          var serializer = new XmlSerializer(ExportConfig.GetType());
          //StringBuilder StringResult = new StringBuilder(XMLStored);
          // Load the XML from the Query into the StringBuilder
  
          // Now we need to build a Stream from the String to use in the XMLReader
          byte[] byteArray = Encoding.UTF8.GetBytes(XMLStored);
          MemoryStream stream = new MemoryStream(byteArray);
          using (var reader = XmlReader.Create(stream))
          {
              ExportConfig = (IIDExportObject)serializer.Deserialize(reader);
          }
      }
  </code></pre>