Let’s say I have a table Employee like this
EmpID, EmpName
1 , hatem
and I write a query: select * from Employee for xml auto
so the output will be in XML format.
I want to know how can I export the result to a XML file to be saved on my computer’s drive as I need to read the XML files from this folder and deserialize them in my .net application.
If you only need to store the XML and not do anything else to it, this is probably the easiest way to accomplish this – using straight simple ADO.NET:
This will create a file
D:\test.xml(or change that to match your system) and will put those XML tags into that file.The
SqlCommandobject also has a.ExecuteXmlReader()method which would return anXmlReaderobject to scan and manipulate the XML – not just return a string. Use whatever makes the most sense to you!PS: also, the output of
FOR XML AUTOis a bit …. let’s say … suboptimal. It uses thedbo.Employeeas it’s main XML tag and so forth… with SQL Server 2008, I would strongly recommend you look into usingFOR XML PATHinstead – it allows you to tweak and customize the layout of the XML output.Compare your original XML output with
FOR XML AUTOagainst this query – just to see the difference:
Output is: