XML is a general purpose tag based language and very easy to transfer and store data across applications. The .Net Framework provides the Classes for read, write, and other operations in XML formatted files .
The following C# program shows how to create an XML file from an Excel file content . Here we are using an OleDbConnection to read the excel file and store the content to a Dataset . Call the method WriteXml of Dataset to write to the XML file.
using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;
using System.Data;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
System.Data.OleDb.OleDbConnection MyConnection ;
System.Data.DataSet ds ;
System.Data.OleDb.OleDbDataAdapter MyCommand ;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='Specify path here \\xl2xml.xls';Extended Properties=Excel 8.0;");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
MyCommand.TableMappings.Add("Table", "Product");
ds = new System.Data.DataSet();
MyCommand.Fill(ds);
MyConnection.Close();
ds.WriteXml("Product.xml");
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
}
}