CREATE AN XML FILE FROM SQL IN C#

john rob

Total Post:108

Points:756
Posted by  john rob
C# 
C#
 1473  View(s)
Ratings:
Rate this:
Hi Developers,

How to create XML file form SQL in C#?

Thanks in advance.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Create an XML file from SQL in C#

    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());
     
                }
            }
        }
    }

      Modified On Mar-30-2018 04:08:53 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!