How to convert datatable to xml in c sharp

Using below example you can convert dataset or data table into XML format. Please find below example for reference.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace convert_datatable_to_xml
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating datatable
            DataTable dt = new DataTable("Employ");
            //adding columns to datatable
            dt.Columns.Add("id");
            dt.Columns.Add("name");

            //adding rows to datatable
            dt.Rows.Add("1", "ranga");
            dt.Rows.Add("2", "rajesh");

            //creating dataset
            DataSet ds = new DataSet("Employ_Details");
            //adding datatable to dataset
            ds.Tables.Add(dt);
            
            //showing dataset value into XML format. 
            Console.WriteLine(ds.GetXml());
            Console.Read();
        }
    }
}
output:

Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar