Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C# 4.0
Tip/Trick

To Generate XML File from Datatable using LINQ

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Feb 2014CPOL1 min read 25.5K   606   13   5
This tip shows how can we generate and save XML files
Image 1

Introduction

Using this simple code, you can generate XML files from database tables. XML files are very useful when it comes to transport data on the wire as they are platform independent. LINQ gives us a very simple way to generate XML files.  

Using the Code

I have shown the code to generate the XML file from a database table tbl_Employee. Let us have a look at the code briefly:

  1. XElement – This class loads and parses the XML.
  2. The string Employees” is the root of the XML.
  3. empList in dt.AsEnumerable dt is the datatable that has the employee details, we convert it to Enumerable and empList is used like an alias.
  4. Select query is pretty straight forward. “Employee” is the tag name of the individual elements. XAttribute indicates the attributes. Hence, here salary and designation are the attributes for Employee tag.
  5. EmployeeName and Designation are child elements for Employee element.
  6. To save this XML on the disk, remove .ToString() and append .Save(<your>)</your><your>. For example .Save(“D://LINQ”)
C#
string s=  new XElement("Employees",
                 from empList in dt.AsEnumerable()
                 orderby empList.Field<decimal>("ESalary") descending
                 select new XElement("Employee",
                      new XAttribute("EmployeeId", empList.Field<Int32>("EID")),
                      new XAttribute("Salary", empList.Field<decimal>("ESalary")),
                      new XElement("EmployeeName", empList.Field<string>("EName")),
                      new XElement("Designation", empList.Field<string>("EDesignation"))
                 )).ToString() 

You can refer to the attached code. It is a simple Windows form that fetches data from SQL server DB and displays it in XML format in the textbox.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIMHO there is an easier way Pin
kotesla23-May-18 22:15
kotesla23-May-18 22:15 
GeneralNice start Pin
Pranay Rana23-Feb-14 22:59
professionalPranay Rana23-Feb-14 22:59 
GeneralRe: Nice start Pin
puja1119123-Feb-14 23:09
puja1119123-Feb-14 23:09 
GeneralRe: Nice start Pin
Pranay Rana23-Feb-14 23:13
professionalPranay Rana23-Feb-14 23:13 
Well i m not legend

But the best thing is you can read other article and you can see the way people presenting the things thats the important thing you can learn...and that will change way of writing article...
GeneralRe: Nice start Pin
puja1119124-Feb-14 2:11
puja1119124-Feb-14 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.