Monday, March 11, 2013

To insert and update the data from XML files into database


To insert and update the data  into database from XML files which are placed in a folder


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ReadXML
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "D:";
            string Filename = "";
            String FileNam = "";
            DirectoryInfo dir = new DirectoryInfo(path);

            foreach (FileInfo flInfo in dir.GetFiles("*.xml"))
            {
                try
                {
                    SqlCommand cmd1 = null;
                    object obj1=null;
                    Filename = flInfo.FullName;
                    FileNam = flInfo.Name;

                   //xml data into Dataset
                  //  DataSet dsData = new DataSet();
                   // dsData.ReadXml(Filename);

                    XmlDataDocument xmldoc = new XmlDataDocument();
                    xmldoc.Load(Filename);
                    XmlElement root = xmldoc.DocumentElement; // to know the root tag

                // To check the data in db whether it exist or not
                    for (int i = 0; i < root.ChildNodes.Count; i++)
                    {
                        if (root.ChildNodes.Item(i).Name.ToString() == "Record")
                        {
                            string Name = root.ChildNodes.Item(i).FirstChild.Name;
                            string Value = root.ChildNodes.Item(i).FirstChild.InnerText;
                            string query = "SELECT COUNT(*) FROM " + root.Name + " WHERE " + Name + "=" + Value;
                            string strConnection = ConfigurationManager.ConnectionStrings["HMSConnectionString"].ToString(); // To read Connection String from app.config
                            SqlConnection connection = new SqlConnection(strConnection);
                            connection.Open();
                            SqlCommand cmd = new SqlCommand(query, connection);
                            object obj = cmd.ExecuteScalar();
                            connection.Close();
                          // To insert the xml data into db
                            if (obj.ToString() == "0")
                            {
                                string InsQuery = "INSERT INTO " + root.Name + " (";
                                string InsFileds = "";
                                string InsFiledValues = "";
                                for (int j = 0; j < root.ChildNodes.Item(i).ChildNodes.Count; j++)
                                {
                                    string ElName = root.ChildNodes.Item(i).ChildNodes.Item(j).Name;
                                    string ELValue = root.ChildNodes.Item(i).ChildNodes.Item(j).InnerText;
                                    InsFileds = InsFileds + ElName + ", ";
                                    InsFiledValues = InsFiledValues +"'"+ ELValue + "', ";
                                }
                                InsFileds = InsFileds.Substring(0,InsFileds.Length-2);

                                InsQuery = InsQuery + InsFileds + ") VALUES (" + InsFiledValues.Substring(0, InsFiledValues.Length - 2) + ")";
                                connection.Open();
                                cmd1 = new SqlCommand(InsQuery, connection);
                                obj1 = cmd1.ExecuteScalar();
                                connection.Close();
                            }

                         // To update the xml data into db
                            else
                            {
                                string UpdQuery = "UPDATE " + root.Name + " SET ";
                                string UpdFileds = "";
                                string UpdFiledValues = "";
                                for (int j = 0; j < root.ChildNodes.Item(i).ChildNodes.Count; j++)
                                {                                  
                                    string ElName = root.ChildNodes.Item(i).ChildNodes.Item(j).Name;
                                    string ELValue = root.ChildNodes.Item(i).ChildNodes.Item(j).InnerText;
                                    if (ElName != Name)
                                    {
                                        UpdFileds = UpdFileds + ElName + " = '" + ELValue + "', ";
                                    }
                                }
                                UpdQuery = UpdQuery + UpdFileds.Substring(0, UpdFileds.Length - 2) + " WHERE  " + Name + "='" + Value + "'";
                                connection.Open();
                                cmd1 = new SqlCommand(UpdQuery, connection);
                                obj1 = cmd1.ExecuteScalar();
                                connection.Close();
                            }
                        }
                    }
                }
                catch (Exception es)
                {
                    throw es;
                }

            }
        }

    }
}

No comments:

Post a Comment