SSIS – Sql Server to XML – Save to file

As you know there is no XML destination in SSIS.

so how you can save result of a query from sql server to XML file?

Solution is:

First Of all you can use FOR XML to get result of query in XML , look at our sample query :

SELECT EmployeeID,NationalIDNumber,ContactID
FROM HumanResources.Employee
FOR XML RAW(‘Facility’),Root(‘Extract’),Elements 

this will creates elements with name ‘Facility’, and attributes ‘EmployeeID’ , ‘NationalIDNumber’ , ‘ContactID’

and the Root node is ‘Extract’.

for more information about FOR XML in sql server look here .

So, start with SSIS:

1- Create a Variable of String type in package scope, and name it as XMLData.


2- Add an Execute SQL Task, set connection as OLEDB to the AdventureWorks Database ( If you haven’t AdventureWorks sample database, download it from here ),

write this query in SqlStatement:

SELECT EmployeeID,NationalIDNumber,ContactID
FROM HumanResources.Employee
FOR XML RAW(‘Facility’),Root(‘Extract’),Elements 


Set ResultSet property to XML.

then go to Result Set tab, and do this mapping:

Result Name                         Variable Name

——————————————–

 0                                      User::XMLData

3- Add a Script Task after execute sql task, set language as C#. and set ReadOnlyVariables as User::XMLData .

then edit script and write this code in Main() method:

public void Main()
        {
            System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
            xdoc.InnerXml = Dts.Variables["XMLData"].Value.ToString();
            xdoc.Save(@"E:\Output.xml");

            Dts.TaskResult = (int)ScriptResults.Success;
        }

 whole schema:

Finished !

now just run the package and result of query will save in xml file specified.



Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
Reza Rad
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

Leave a Reply