Please enable Javascript to correctly display the contents on Dot Net Tricks!

Create xml from database using LINQ

  Author : Shailendra Chauhan
Updated On : 24 Jun 2014
Total Views : 52,277   
 

XML data is often used to transfer data from one application to another. Since XML is language independent, hence it is a good choice to use xml for transferring data between two different platform applications. In C#, it is easy to make xml from database by using LINQ to SQL and LINQ to XML.

Create XML from Database

Suppose we have below table in database. Now I want to make xml of all the products which have stock greater than 0. Here I am using LINQ Pad to query data from the database. It is a great tool to query data from database using LINQ to SQL, LINQ to XML, LINQ to Entity Framework.

 CREATE TABLE Product (
 ProductID int IDENTITY(1,1) NOT NULL,
 ProductName varchar(50) NOT NULL,
 Price float NOT NULL,
 Stock int NOT NULL
)
GO
INSERT INTO Product (ProductName,Price,Stock)VALUES('P001',12.12,100)
INSERT INTO Product (ProductName,Price,Stock)VALUES('P002',102.12,200)
INSERT INTO Product (ProductName,Price,Stock)VALUES('P003',104.12,500)
INSERT INTO Product (ProductName,Price,Stock)VALUES('P004',108.12,100)
INSERT INTO Product (ProductName,Price,Stock)VALUES('P005',72.12,10)
INSERT INTO Product (ProductName,Price,Stock)VALUES('P006',72.12,0)
GO
SELECT * FROM Product 

Now, query the data from above table using LINQ. We can also save the output to xml file.

 //Export above xml to xmlfile
XElement.Save(Server.MapPath(@"~/export.xml")); 
Summary
In this article I try to explain how to create xml from database using LINQ with example. I hope after reading this article you will be able to create xml from database. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+