How to create XML file from Excel

XML is a versatile and widely used tag-based language that offers seamless data transfer and storage capabilities across diverse applications. Within the .NET Framework, developers have access to a comprehensive set of classes specifically designed for reading, writing, and performing various operations on XML-formatted files. These classes provide a robust and efficient framework for handling XML data, ensuring compatibility, and facilitating effective integration with other components within the .NET ecosystem. Using these classes, developers can confidently manipulate XML data, extract relevant information, and perform a wide range of operations to meet their application requirements.

XML file from Excel

In the following C# program, we demonstrate the process of creating an XML file from the content of an Excel file. To achieve this, we utilize an OleDbConnection to establish a connection and read the Excel file, extracting the content and storing it within a Dataset. Subsequently, we employ the WriteXml method of the Dataset to write the extracted content to an XML file. This approach enables seamless conversion and preservation of the data from the Excel file into an XML format, facilitating interoperability and further data processing capabilities.

Full Source C#
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()); } } } }

Click here to download the input excel file xl2xml.xls