Is there some reason that you're willing to take the performance hit involved in using middleware to do a job that the database is designed to do for you? It certainly can be done, but most designers prefer the faster response time offered by using the database to solve this kind of problem.
The short answer is to use your middleware if possible to create the XML file. Have the middleware execute the SQL, then stream the result to a file. IF you can't do that, you can use OSQL.EXE to create the file too, but you'll almost certainly have problems using it.
-PatP