Introduction
Sometimes it is required to fetch table data into a file or in an application for transferring data over the network or for some application. This blog describes and demonstrates, how to export SQL table data into XML form using SQL queries.
Getting Started
SQL Server provides some T-SQL command which supports to export data in XML format. It provides four most used methods(AUTO, RAW, PATH & EXPLICIT) to export table content into XML format. All the method are used with FOR XML clause as postfix.
Syntax:
SELECT 'Columns' FROM 'Table_Name' FORM XML 'Method_Name'
Before going to discuss the export methods of SQL, I will introduce a table that I have already created for taking as an example while discussing methods.
I have created a table having name'StudentDetails' and add some columns using the below queries and also have inserted some data into the table to took as an example.
Codes:
CREATE TABLE StudentDetails
(
StudentID INT,
StudentName NVARCHAR(100),
StudentClass NVARCHAR(10)
)
INSERT INTO StudentDetails VALUES(1,'Kailash1','5TH')
INSERT INTO StudentDetails VALUES(2,'Kailash2','5TH')
INSERT INTO StudentDetails VALUES(3,'Kailash3','5TH')
INSERT INTO StudentDetails VALUES(4,'Kailash3','5TH')
INSERT INTO StudentDetails VALUES(5,'Kailash5','5TH')
Export Methods:
- RAW
The RAW method exports each rows of table as a XML element and each column of that rows as attributes of element and the element name by default is <row>.
Syntax:
Example:SELECT 'Columns' FROM 'Table_Name' FORM XML RAW
Result:SELECT * FROM StudentDetails FOR XML RAW
<row StudentID="1" StudentName="Kailash1" StudentClass="5TH"/> <row StudentID="2" StudentName="Kailash2" StudentClass="5TH"/> <row StudentID="3" StudentName="Kailash3" StudentClass="5TH"/> <row StudentID="4" StudentName="Kailash3" StudentClass="5TH"/> <row StudentID="5" StudentName="Kailash5" StudentClass="5TH"/>
- AUTO
The AUTO mode method work same as RAW method, but it names the element name same as the table name. for example here we have created a table named 'StudentDetails', hence the element name will be <StudentDetails>.
Syntax:
Example:SELECT 'Columns' FROM 'Table_Name' FORM XML AUTO
Result:SELECT * FROM StudentDetails FOR XML AUTO
<StudentDetails StudentID="1" StudentName="Kailash1" StudentClass="5TH"/> <StudentDetails StudentID="2" StudentName="Kailash2" StudentClass="5TH"/> <StudentDetails StudentID="3" StudentName="Kailash3" StudentClass="5TH"/> <StudentDetails StudentID="4" StudentName="Kailash3" StudentClass="5TH"/> <StudentDetails StudentID="5" StudentName="Kailash5" StudentClass="5TH"/>
- PATH
The path mode is little difference from AUTO & RAW method, it generates nested elements each columns of row even you can add root element for a all row. Means It generates an element for a row and child element for row columns.
Syntax:
Example:-1SELECT 'Columns' FROM 'Table_Name' FORM XML PATH
Using PATH method without any parameter
Result:SELECT * FROM StudentDetails FOR XML PATH
Example:-2<row><StudentID>1</StudentID><StudentName>Kailash1</StudentName><StudentClass>5TH</StudentClass></row> <row><StudentID>2</StudentID><StudentName>Kailash2</StudentName><StudentClass>5TH</StudentClass></row> <row><StudentID>3</StudentID><StudentName>Kailash3</StudentName><StudentClass>5TH</StudentClass></row> <row><StudentID>4</StudentID><StudentName>Kailash3</StudentName><StudentClass>5TH</StudentClass></row> <row><StudentID>5</StudentID><StudentName>Kailash5</StudentName><StudentClass>5TH</StudentClass></row>
By default the path method gives element name generated for a row as <row>, but can be changed by providing name as parameter.
Result:SELECT * FROM StudentDetails FOR XML PATH('StudentDetails')
Example:-3<StudentDetails><StudentID>1</StudentID><StudentName>Kailash1</StudentName><StudentClass>5TH</StudentClass></StudentDetails> <StudentDetails><StudentID>2</StudentID><StudentName>Kailash2</StudentName><StudentClass>5TH</StudentClass></StudentDetails> <StudentDetails><StudentID>3</StudentID><StudentName>Kailash3</StudentName><StudentClass>5TH</StudentClass></StudentDetails> <StudentDetails><StudentID>4</StudentID><StudentName>Kailash3</StudentName><StudentClass>5TH</StudentClass></StudentDetails> <StudentDetails><StudentID>5</StudentID><StudentName>Kailash5</StudentName><StudentClass>5TH</StudentClass></StudentDetails>
Adding Top-level element
Result:SELECT * FROM StudentDetails FOR XML PATH('StudentDetails'), root ('Root')
<StudentDetails><StudentID>1</StudentID><StudentName>Kailash1</StudentName><StudentClass>5TH</StudentClass></StudentDetails> <StudentDetails><StudentID>2</StudentID><StudentName>Kailash2</StudentName><StudentClass>5TH</StudentClass></StudentDetails> <StudentDetails><StudentID>3</StudentID><StudentName>Kailash3</StudentName><StudentClass>5TH</StudentClass></StudentDetails> <StudentDetails><StudentID>4</StudentID><StudentName>Kailash3</StudentName><StudentClass>5TH</StudentClass></StudentDetails> <StudentDetails><StudentID>5</StudentID><StudentName>Kailash5</StudentName><StudentClass>5TH</StudentClass></StudentDetails>
- EXPLICIT
The EXPLICIT method provides more control to export table data into XML format. The EXPLICIT mode query must be written in a specific way so that the additional information about the required XML, such as expected nesting in the XML, is explicitly specified as part of the query. However, EXPLICIT mode provides the most flexibility in generating the XML you want from a query result.
This is very vast concept you can refer this link for the same.
Related Articles
- Export DataTable To Excel in C#
- Export to Excel in MVC
- Export HTML Table to Excel Using JavaScript
- Validating Excel Sheet in C#
- Import Excel in C#
- Import XML into SQL Table
- Reading Excel file in AngularJS
- Creating controller for File Upload
Thanks
Tags
SQL