title: SQL Server 2008 - Add XML Declaration to XML Output
body: <p>I've been battling with this one for a few days now, I'm looking to automate an XML output with the below syntax</p>
  
  <pre><code> SELECT (
     SELECT CONVERT(VARCHAR(10),GETDATE(),103)
     FOR XML PATH('DataVersion'), 
       TYPE
     ),
     (  
     SELECT CoNum,
       CoName,
       CONVERT(VARCHAR(10),AccountToDate,103) 'DLA',
       LAFileNet
     FROM @XMLOutput  
     FOR XML PATH('Company'),
       TYPE  
     )
   FOR XML PATH(''),
     ROOT('Companies')
  </code></pre>
  
  <p>Which creates the below output</p>
  
  <pre><code>&lt;Companies&gt;
    &lt;DataVersion&gt;15/11/2010&lt;/DataVersion&gt;
    &lt;Company&gt;
      &lt;CoNum&gt;111&lt;/CoNum&gt;
      &lt;CoName&gt;ABCLmt&lt;/CoName&gt;
      &lt;DLA&gt;12/12/2010&lt;/DLA&gt;
      &lt;LAFileNet&gt;1234&lt;/LAFileNet&gt;
    &lt;/Company&gt;
    &lt;Company&gt;
      &lt;CoNum&gt;222&lt;/CoNum&gt;
      &lt;CoName&gt;DEFLmt&lt;/CoName&gt;
      &lt;DLA&gt;12/12/2007&lt;/DLA&gt;
      &lt;LAFileNet&gt;5678&lt;/LAFileNet&gt;
    &lt;/Company&gt;
  &lt;/Companies&gt;
  </code></pre>
  
  <p>What I'm struggling with is how to add the XML declaration <code>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;</code> to the top of the output??</p>
  
  <p>Update 1: Would I be correct in thinking I need to create a XML schema in SQL server to define the xsl:output. Then assign the output to that schema? </p>
  
  <p>Update 2: Have since found these links
  <a href="http://forums.asp.net/t/1455808.aspx" rel="noreferrer">http://forums.asp.net/t/1455808.aspx</a> -- Check out comment from Jian Kang. Also
  <a href="http://www.devnewsgroups.net/group/microsoft.public.sqlserver.xml/topic60022.aspx" rel="noreferrer">http://www.devnewsgroups.net/group/microsoft.public.sqlserver.xml/topic60022.aspx</a></p>