FOR XML is generating a empty first line I'm parsing with flash a XML file generated by this code:

:XML ON  
USE MyDatabaseName 
GO 
SET NOCOUNT ON 

SELECT * FROM ProgramacionDia as programa order by hora 
FOR XML AUTO,
ROOT ('toflash'), 
ELEMENTS 

SET NOCOUNT OFF


But I get a XML file with the first line empty. Removing this first empty line in the generated XML works ok with flash, but with the generated XML no.

How can I remove that line? Is my script wrong? I have no much idea about this code.

I'm running SQL Server 9.0.
After a bit of trial and error, I found this is caused by the USE statement occuring after XML mode is turned on. I have no idea why; I assume it's simply an artifact of sqlcmd processing.

Here are three solutions I came up with from playing around with the code:


Remove the USE completely, and specify the database name on the command line using the -d parameter instead.

:XML ON  

SET NOCOUNT ON 

SELECT * FROM ProgramacionDia as programa order by hora 
    FOR XML AUTO, ROOT ('toflash'), ELEMENTS 

SET NOCOUNT OFF

Remove the USE completely, and three-part-name the table.

:XML ON  

SET NOCOUNT ON 

SELECT * FROM [MyDatabaseName]..[ProgramacionDia] as programa order by hora 
    FOR XML AUTO, ROOT ('toflash'), ELEMENTS 

SET NOCOUNT OFF

Move the USE statement above the :XML ON directive, and delete the GO (this will suppress the "Changed database context to 'MyDatabaseName'" message in the output). I have no idea why this also fixes the issue.

USE MyDatabaseName

:XML ON  

SET NOCOUNT ON 

SELECT * FROM ProgramacionDia as programa order by hora 
    FOR XML AUTO, ROOT ('toflash'), ELEMENTS 

SET NOCOUNT OFF



Depending on what your actual script looks like, one of these options should be adaptable for you.