How to create Excel file from XML

XML, being a platform-independent language, offers the significant advantage of facilitating the interchangeability of information across various platforms and operating systems. When information is formatted in XML, it becomes universally accessible, enabling seamless utilization and interpretation on diverse platforms. Moreover, XML is characterized by its self-describing nature, encompassing both the data itself and the accompanying rules that allow for the precise identification and interpretation of the contained information. This distinctive feature empowers developers and applications to effectively understand and extract meaningful data from XML documents, enhancing interoperability and data integration across systems.


In this scenario, our objective is to read the content from an XML file and write the same content to an Excel file. To accomplish this, we employ an XmlReader to read the XML file and populate a Dataset with the retrieved data. Subsequently, we iterate through the Dataset and transfer the content to the Excel file, ensuring seamless data transfer and preservation of the original structure. This process allows for efficient extraction of data from the XML file and its representation in an Excel format, providing compatibility and versatility for further data analysis and manipulation.

For create an excel file you have to add reference of Excel library to you project , follow the link C# Create Excel File for details.

Full Source C#
using System; using System.Data; using System.Windows.Forms; using System.Xml; using System.Data; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; DataSet ds = new DataSet(); XmlReader xmlFile ; int i = 0; int j = 0; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings()); ds.ReadXml(xmlFile); for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++) { xlWorkSheet.Cells[i + 1, j + 1] = ds.Tables[0].Rows[i].ItemArray[j].ToString (); } } xlWorkBook.SaveAs("Specify path here\\xml2excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlApp); releaseObject(xlWorkBook); releaseObject(xlWorkSheet); MessageBox.Show("Done .. "); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; } finally { GC.Collect(); } } } }

Click here to download the input file product.xml : product.xml