title: NULL elements in FOR XML Clause / Alternative to XSINIL
body: <p>I have some legacy code similar to:</p>
  
  <pre><code> ...
   '&lt;field1&gt;' +   
   case when field1 is null then '' else cast( field1 as varchar ) end +   
   '&lt;/field1&gt;' +  
   ...
  </code></pre>
  
  <p>Which generates the following XML for empty elements:</p>
  
  <pre><code> ....
   &lt;field1&gt;&lt;/field1&gt;
   ...
  </code></pre>
  
  <p>And I'm replacing the query with FOR XML:</p>
  
  <pre><code> SELECT field1, 
   ...
   FOR XML RAW, ELEMENTS
  </code></pre>
  
  <p>Now, this does not output an element for columns with NULL values.  I know about XSINIL:</p>
  
  <pre><code> FOR XML RAW, ELEMENTS XSINIL
  </code></pre>
  
  <p>But this generates a namespaced empty XML element, which is not compatible with the legacy code reading this output.</p>
  
  <pre><code> ...
   &lt;field1 xsi:nil="true" /&gt;
   ...
  </code></pre>
  
  <p>Any suggestions on generating the format below while still using the FOR XML Clause?</p>
  
  <pre><code> ....
   &lt;field1&gt;&lt;/field1&gt;
   ...
  </code></pre>
  
  <p>Thanks!</p>