Wednesday, 20 May 2015

Console application using C# to Convert Excel file data into xml file with adding own tag element like attributes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web;
using System.Data;
using System.Xml;


namespace ConvertExceltoXMLConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            string Exfilepath = @"D:\Verification.xlsx";
            DataTable dt = new DataTable();
            DataRow dr = null;
            string fetch = null;
            System.Data.OleDb.OleDbConnection MyConnection = null;
            System.Data.DataSet DtSet = null;
            System.Data.OleDb.OleDbDataAdapter MyCommand = null;
            // MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + Exfilepath + "';Extended Properties=Excel 8.0;");

            //If you MS Excel 2007 then use below lin instead of above line
            MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Exfilepath + "';Extended Properties=Excel 12.0;");

            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select ApplicantFirstName,VoterId,ExternalApplicationId,DateOfBirth,Gender,Address1Line from [VoterID$]", MyConnection);
            DtSet = new System.Data.DataSet();
            MyCommand.Fill(DtSet, "[VoterID$]");


            XmlDocument xmlDoc = new XmlDocument();
            XmlNode docNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmlDoc.AppendChild(docNode);
            XmlNode rootNode = xmlDoc.CreateElement("DCRequest");
            XmlAttribute attribute1 = xmlDoc.CreateAttribute("xmlns");
          
            attribute1.Value = "http://swapnilmahangare.blogspot.in";
            rootNode.Attributes.Append(attribute1);
            xmlDoc.AppendChild(rootNode);

            XmlNode AuthenticationNode = xmlDoc.CreateElement("Authentication");
            XmlAttribute AuthenticationAttribute = xmlDoc.CreateAttribute("type");
            AuthenticationAttribute.Value = "OnDemand";
            AuthenticationNode.Attributes.Append(AuthenticationAttribute);
            rootNode.AppendChild(AuthenticationNode);

            XmlNode UserIdNode = xmlDoc.CreateElement("UserId");
            UserIdNode.AppendChild(xmlDoc.CreateTextNode("swapnil"));
            AuthenticationNode.AppendChild(UserIdNode);
            XmlNode PasswordNode = xmlDoc.CreateElement("Password");
            PasswordNode.AppendChild(xmlDoc.CreateTextNode("*******"));
            AuthenticationNode.AppendChild(PasswordNode);

            XmlNode RequestInfoNode = xmlDoc.CreateElement("RequestInfo");
            rootNode.AppendChild(RequestInfoNode);

            XmlNode SolutionSetNode = xmlDoc.CreateElement("SolutionSetId");
            SolutionSetNode.AppendChild(xmlDoc.CreateTextNode("665"));
            RequestInfoNode.AppendChild(SolutionSetNode);
            XmlNode SolutionSetVersionNode = xmlDoc.CreateElement("SolutionSetVersion");
            SolutionSetVersionNode.AppendChild(xmlDoc.CreateTextNode("V2"));
            RequestInfoNode.AppendChild(SolutionSetVersionNode);
            XmlNode ExecutionNode = xmlDoc.CreateElement("ExecutionMode");
            ExecutionNode.AppendChild(xmlDoc.CreateTextNode("NewWithContext"));
            RequestInfoNode.AppendChild(ExecutionNode);


            DataTable dt1 = DtSet.Tables[0];
            for (int i = 0; i < dt1.Rows.Count; i++)
            {

                if (!string.IsNullOrWhiteSpace(dt1.Rows[i][1].ToString()))
                {
                    XmlNode FieldsNode = xmlDoc.CreateElement("Fields");
                    rootNode.AppendChild(FieldsNode);
                    XmlNode userNode = xmlDoc.CreateElement("Field");
                    XmlAttribute attribute = xmlDoc.CreateAttribute("Key");
                    attribute.Value = "ApplicantFirstName";
                    userNode.Attributes.Append(attribute);
                    userNode.InnerText = dt1.Rows[i][0].ToString();
                    FieldsNode.AppendChild(userNode);

                    userNode = xmlDoc.CreateElement("Field");
                    attribute = xmlDoc.CreateAttribute("Key");
                    attribute.Value = "VoterId";
                    userNode.Attributes.Append(attribute);
                    userNode.InnerText = dt1.Rows[i][1].ToString();
                    FieldsNode.AppendChild(userNode);


                    userNode = xmlDoc.CreateElement("Field");
                    attribute = xmlDoc.CreateAttribute("Key");
                    attribute.Value = "ExternalApplicationId";
                    userNode.Attributes.Append(attribute);
                    userNode.InnerText = dt1.Rows[i][2].ToString();
                    FieldsNode.AppendChild(userNode);

                    userNode = xmlDoc.CreateElement("Field");
                    attribute = xmlDoc.CreateAttribute("Key");
                    attribute.Value = "DateOfBirth";
                    userNode.Attributes.Append(attribute);
                    userNode.InnerText = dt1.Rows[i][3].ToString();
                    FieldsNode.AppendChild(userNode);

                    userNode = xmlDoc.CreateElement("Field");
                    attribute = xmlDoc.CreateAttribute("Key");
                    attribute.Value = "Gender";
                    userNode.Attributes.Append(attribute);
                    userNode.InnerText = dt1.Rows[i][4].ToString();
                    FieldsNode.AppendChild(userNode);

                    userNode = xmlDoc.CreateElement("Field");
                    attribute = xmlDoc.CreateAttribute("Key");
                    attribute.Value = "Address1Line";
                    userNode.Attributes.Append(attribute);
                    userNode.InnerText = dt1.Rows[i][5].ToString();
                    FieldsNode.AppendChild(userNode);
                }
            }

            xmlDoc.Save("D:\\Verification.xml");

     
        }
    }
}



Output:-

 <?xml version="1.0" encoding="UTF-8" ?>
 <DCRequest xmlns="http://swapnilmahangare.blogspot.in">
 <Authentication type="OnDemand">
  <UserId>swapnil</UserId>
  <Password>*******</Password>
  </Authentication>
 <RequestInfo>
  <SolutionSetId>33</SolutionSetId>
  <SolutionSetVersion>rr</SolutionSetVersion>
  <ExecutionMode>NewWithContext</ExecutionMode>
  </RequestInfo>
 <Fields>
  <Field Key="ApplicantFirstName">REJITHA M</Field>
  <Field Key="VoterId">232323</Field>
  <Field Key="ExternalApplicationId">2323</Field>
  <Field Key="DateOfBirth">08.10.1991</Field>
  <Field Key="Gender">FEMALE</Field>
  <Field Key="Address1Line">rrrrrrr</Field>
  </Fields>
 <Fields>
  <Field Key="ApplicantFirstName">RAKESH T</Field>
  <Field Key="VoterId">343434</Field>
  <Field Key="ExternalApplicationId">55555</Field>
  <Field Key="DateOfBirth">04.07.1991</Field>
  <Field Key="Gender">MALE</Field>
  <Field Key="Address1Line">gggggg</Field>
  </Fields>
</DCRequest>

No comments:

Post a Comment