Convert SQL data to XML

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           
          
           
            try
            {
                string conn = @"Data Source=Admin123; database=etltest;uid=sa;pwd=password";
                SqlConnection myConn = new SqlConnection(conn);
                SqlCommand myCmd = new SqlCommand();
                string query = "select  * from D_EMPLOYEE_INFO";

                SqlDataAdapter myAdp = new SqlDataAdapter(query, myConn);

                DataSet dsData = new DataSet();
              
                myConn.Open();
               
                myAdp.Fill(dsData);
                dsData.Tables[0].TableName = "D_employee";

                string appPath = @"C:";



                    if (!Directory.Exists(appPath + "\\XmlDocs"))
                        System.IO.Directory.CreateDirectory(appPath + "\\XmlDocs");

                    string xmlDocName = appPath + "\\XmlDocs\\"  + dsData.Tables[0].TableName + ".xml";
                    if (File.Exists(appPath + "\\XmlDocs\\"  + dsData.Tables[0].TableName + ".xml"))
                        File.Delete(appPath + "\\XmlDocs\\"  + dsData.Tables[0].TableName + ".xml");

                    StreamWriter myStreamWriter = new StreamWriter(xmlDocName);
                    dsData.Tables[0].WriteXml(myStreamWriter);
                    myStreamWriter.Close();
              
                myConn.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


        }
    }
}

No comments:

Post a Comment