Articles in categories

Articles

Data from sql table to xml

I needed to retrieve some data from an sql server table and put it in an xml file. That data I wanted to use in my application, but because I wasn't allowed to connect with the database directly I needed a way to create an xml file.

This is how I need the xml file to look like (dummy data):

Sql_to_xml

And this is the sql query that will create it:

SELECT * 
FROM Person
FOR XML AUTO, ROOT ('Persons'), ELEMENTS

You can do a lot more with the FOR XML statement. More info can be found here: Basic Syntax of the FOR XML Clause.

Maybe I'll add some more examples later.