In a few of the early sessions of the XML Workshop, we have seen how to generate XML results from TSQL. We have examined the FOR XML clause and have seen the usages of RAW, AUTO, PATH and EXPLICIT.
XML Workshop 1 - Explains FOR XML with AUTO and RAW
XML Workshop 3 - Explains FOR XML with PATH
XML Workshop 4 - Explains FOR XML with EXPLICIT
Accessing results of FOR XML from a .NET application
Most of the times the results of FOR XML queries are expected to be consumed by client applications. In this session, let us see how to access the results of FOR XML queries from VB.NET and C#.NET applications using ADO.NET.
Sample Table and Stored Procedure
Let us create a sample table and populate it with some data. Let us then create a stored procedure which generates an XML document using FOR XML.
-- Let us create a new database
CREATE DATABASE XmlTest
-- Let us now create a table for our example
CREATE TABLE Employee (EmpID INT IDENTITY(1,1), EmpName VARCHAR(50) )
-- let us insert some data
INSERT INTO Employee (EmpName)
SELECT 'Jacob' UNION ALL
SELECT 'Michael' UNION ALL
Let us now create a stored procedure which generates the XML document that we need.
CREATE PROCEDURE GetEmployeeData
SELECT * FROM Employee
FOR XML AUTO, ROOT('Employees')
Here is the result of the stored procedure.
<Employee EmpID="1" EmpName="Jacob" />
<Employee EmpID="2" EmpName="Michael" />
<Employee EmpID="3" EmpName="Richard" />
VB.NET Sample Code
Let us create a VB.NET console application which executes the above stored procedure and fetches the XML document. Here is the code which does that.
'let us make a connection first
Dim str As String
str = "Data Source=TOSHIBA-USER\SQL2005;Initial Catalog=XmlTest;"
str = str + "Persist Security Info=True;User ID=sa;Password=sa2005"
Dim cn As New SqlConnection(str)
'Let us make a command
Dim cmd As New SqlCommand()
cmd.Connection = cn
cmd.CommandText = "GetEmployeeData"
cmd.CommandType = CommandType.StoredProcedure
'What we need next is an XMLReader and call
'ExecuteXMLReader method of SqlCommand.
Dim r As XmlReader
r = cmd.ExecuteXmlReader()
'Read the data from XMLReader and Load into
'the String Builder
Dim xmlData As New StringBuilder
Do While r.Read()
'Print the output
'Close the Reader and DB Connection
C#.NET Sample Code
Now, let us write the C#.NET version of the above code.
static void Main(string args)
//let us make a connection first
str = "Data Source=TOSHIBA-USER\\SQL2005;Initial Catalog=XmlTest;";
str = str + "Persist Security Info=True;User ID=sa;Password=sa2005";
SqlConnection cn = new SqlConnection(str);
//Let us make a command
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "GetEmployeeData";
cmd.CommandType = CommandType.StoredProcedure;
//What we need next is an XMLReader and call
//ExecuteXMLReader method of SqlCommand.
r = cmd.ExecuteXmlReader();
//Read the data from XMLReader and Load into
//the String Builder
StringBuilder xmlData = new StringBuilder();
//Print the output
//Close the Reader and DB Connection
This article presents an example of accessing FOR XML results from a .NET application. I guess there must be other ways of doing this too. This sample code is created for the purpose of demonstrating the basic usage. The sample applications are tested with the sample data and found to be working correctly. However, please note that the code is not highly optimized. You might need slightly different code for a production level application. For example, you might need to dispose() the database connection after reading the information. I leave that to the .NET developer in you.