title: SQL Server - defining an XML type column with UTF-8 encoding
body: <p>The default encoding for an XML type field defined in an SQL Server is UTF-16. I have no trouble inserting into that field with UTF-16 encoded XML streams.</p>
  <p>But if I tried to insert into the field with UTF-8 encoded XML stream, the insert attempt would receive the error response<br />
  <code>unable to switch encoding</code>.</p>
  <p><strong>QUESTION</strong>: Is there a way to define a SQL Server column/field as having UTF-8 encoding?</p>
  <strong>Further info</strong>
  <p>The insertion operations are performed using Spring JDBCTemplate.</p>
  <p>The XML Stream was produced by JAXB Marshaller set to UTF-8 or UTF-16 encoding.</p>
  <pre><code>private String marshall(myDAO myTao, JAXBEncoding jaxbEncoding)
  throws JAXBException{
      JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class);
      m = jc.createMarshaller();
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
      if (jaxbEncoding!=null)
          m.setProperty(Marshaller.JAXB_ENCODING, jaxbEncoding.toString());
      StringWriter strw = new StringWriter();
      m.marshal(myTao, strw);
      String strw.toString();
  }
  </code></pre>
  <p>Where ...</p>
  <pre><code>public enum JAXBEncoding {
      UTF8(&quot;UTF-8&quot;),
      UTF16(&quot;UTF-16&quot;)
      ;
      
      private String value;
      private JAXBEncoding(String value){
          this.value = value;
      }
      
      public String toString(){
          return this.value;
      }
  }
  </code></pre>