Home > DeveloperSection > Forums > Create an XML file from SQL in C#
john rob

Total Post:108

Points:756
Posted on    June-15-2013 5:14 AM

 C# C# 
Ratings:


 1 Reply(s)
 1019  View(s)
Rate this:
Hi Developers,

How to create XML file form SQL in C#?

Thanks in advance.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    June-15-2013 7:26 AM

Hi John,

You can use this code:

using System;

using System.Data;

using System.Windows.Forms;

using System.Xml;

using System.Data.SqlClient;

 

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            string connetionString = null;

            SqlConnection connection );

            SqlDataAdapter adapter ;

            DataSet ds = new DataSet();

            string sql = null;

 

            connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password";

            connection = new SqlConnection(connetionString);

            sql = "select * from Product";

            try

            {

                connection.Open();

                adapter = new SqlDataAdapter(sql, connection);

                adapter.Fill(ds);

                connection.Close();

                ds.WriteXml("Product.xml");

                MessageBox.Show("Done");

            }

            catch (Exception ex)

            {

                MessageBox.Show (ex.ToString());

 

            }

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick