Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Friends,
How can retrieve SQL Query results in a XML format? I means, whenever we fired a Query on Database (either SQLServer, Oracle, MySql etc..) it returns the result in XML format.

actually I have to used it with my ASP.Net web Application and the scenario is , we can't write query like 'XML Auto, Raw, Path' etc. so is there any other way to handle this?.

Thanks.
Posted
Updated 15-Jan-12 20:34pm
v2

SQL
DECLARE @Employee TABLE (id Bigint IDENTITY(1,1), Name NVARCHAR(50))
INSERT INTO @Employee (name) SELECT 'ABC'
INSERT INTO @Employee (name) SELECT 'DEF'
INSERT INTO @Employee (name) SELECT 'GHI'
INSERT INTO @Employee (name) SELECT 'JKL'
INSERT INTO @Employee (name) SELECT 'MNO'


select * from @Employee For XML Path('Employee')


Result will be
XML
<Employee>
  <id>1</id>
  <Name>ABC</Name>
</Employee>
<Employee>
  <id>2</id>
  <Name>DEF</Name>
</Employee>
<Employee>
  <id>3</id>
  <Name>GHI</Name>
</Employee>
<Employee>
  <id>4</id>
  <Name>JKL</Name>
</Employee>
<Employee>
  <id>5</id>
  <Name>MNO</Name>
</Employee>



select * from @Employee For XML RAW('Employee')


Result will be

XML
<Employee id="1" Name="ABC" />
<Employee id="2" Name="DEF" />
<Employee id="3" Name="GHI" />
<Employee id="4" Name="JKL" />
<Employee id="5" Name="MNO" />



select * from @Employee For XML AUTO


Result will be

XML
<_x0040_Employee id="1" Name="ABC" />
<_x0040_Employee id="2" Name="DEF" />
<_x0040_Employee id="3" Name="GHI" />
<_x0040_Employee id="4" Name="JKL" />
<_x0040_Employee id="5" Name="MNO" />
 
Share this answer
 
Comments
ashriv 16-Jan-12 2:34am    
Thanks for reply. but actually I have to used it with my ASP.Net web Application and the scenario is , we can't write query like 'XML Auto, Raw, Path' etc. so is there any other way to handle this?.
RDBurmon 16-Jan-12 2:45am    
Can't you fill the data table and then try to do serialization on that object ?
I think it will help .
Once you have your data in a DataSet, you can always use the WriteXml to get it into XML. I don't want to second guess what you are doing, but why would you want to query the dataset in XML if you already queried it in your SQL?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900