Convert Dataset to XML file using C#.net or ASP.NET

Previous                                                        Home                                                 Next

Convert Dataset to XML file 

XML is very good for transferring data from one platform to other. This post contains information about how can we convert dataset to xml file.Using Dataset.WriteXml method we are converting dataset to xml physical file. 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Dataset_to_xml_file
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //connectionstring
                string strConn="server=localhost;database=rajesh;uid=sa;pwd=admin@123";
                //query for fetching records.
                string query="select * from employeeMaster";
             //creaing dataset object. http://aspnettutorialonline.blogspot.com/
                DataSet objDS = new DataSet();
                using(SqlConnection con=new SqlConnection(strConn))
                {
                    SqlDataAdapter daConn = new SqlDataAdapter(query, con);
                    daConn.Fill(objDS);
                    //write dataset into xml file. 
                    objDS.WriteXml("EmployeeDetails.xml");
                }
                Console.WriteLine("file created");
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

output of the program:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <eno>1</eno>
    <ename>ranga</ename>
    <sal>260000</sal>
    <location>Hyderabad</location>
  </Table>
  <Table>
    <eno>2</eno>
    <ename>swati</ename>
    <sal>330000</sal>
    <location>Bangalore</location>
  </Table><!--http://aspnettutorialonline.blogspot.com/-->
  <Table>
    <eno>3</eno>
    <ename>anil</ename>
    <sal>220000</sal>
    <location>chennai</location>
  </Table>
  <Table>
    <eno>4</eno>
    <ename>john</ename>
    <sal>310000</sal>
    <location>Bangalore</location>
  </Table>
  <Table>
    <eno>5</eno>
    <ename>Kavitha</ename>
    <sal>110000</sal>
    <location>Bangalore</location>
  </Table>
  <Table>
    <eno>6</eno>
    <ename>padma</ename>
    <sal>25000</sal>
    <location>Bangalore</location>
  </Table>
</NewDataSet>




Previous                                                        Home                                                 Next
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