XML files are extensible markup language files, which are encoded in plain text and can be opened by any text editor. Sometimes you may need to do conversions between Excel and XML. In this article, I will demonstrate how to Convert Excel XLS or XLSX to XML and Convert XML to Excel XLS or XLSX in C# and VB.NET using Spire.XLS for .NET API.
Installation
The DLL files of Spire.XLS for .NET API can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then adding the following code:
PM> Install-Package Spire.XLS
Convert Excel XLS or XLSX to XML in C# and VB.NET
The SaveAsXML method of Workbook class is used to convert Excel XLS/XLSX file to XML.
The following are the steps to achieve the conversion:
- Create an instance of Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method (you can also load from stream using Workbook.LoadFromStream() method if you want.).
- Call Workbook.SaveAsXml() method to save the Excel file to XML file format.
C#
using Spire.Xls;
namespace ConvertExcelToXml
{
class Program
{
static void Main(string[] args)
{
//Create an instance of Workbook class
Workbook workbook = new Workbook();
//Load an Excel XLSX file
workbook.LoadFromFile("Sample.xlsx");
//Or load an Excel XLS file
//workbook.LoadFromFile("Sample.xls");
//Save as XML file format
workbook.SaveAsXml("ToXML.xml");
}
}
}
VB.NET
Imports Spire.Xls
Namespace ConvertExcelToXml
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create an instance of Workbook class
Dim workbook As Workbook = New Workbook()
'Load an Excel XLSX file
workbook.LoadFromFile("Sample.xlsx")
'Or load an Excel XLS file
'workbook.LoadFromFile("Sample.xls");
'Save as XML file format
workbook.SaveAsXml("ToXML.xml")
End Sub
End Class
End Namespace
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2021/12/exceltoxml.png?w=800)
Convert XML to Excel XLS or XLSX in C# and VB.NET
The following are the steps to convert an XML file to Excel:
- Create an instance of Workbook class.
- Load an XML file using Workbook.LoadFromXml() file.
- Call Workbook.SaveToFile() method to save the XML file as Excel XLS/XLSX format.
C#
using Spire.Xls;
namespace ConvertXmlToExcel
{
class Program
{
static void Main(string[] args)
{
//Create an instance of Workbook class
Workbook workbook = new Workbook();
//Load an XML file
workbook.LoadFromXml("ToXML.xml");
//Save as XLSX file format
workbook.SaveToFile("ToExcel.xlsx", ExcelVersion.Version2016);
//Save as XLS file format
//workbook.SaveToFile("ToExcel.xls", ExcelVersion.Version97to2003);
}
}
}
VB.NET
Imports Spire.Xls
Namespace ConvertXmlToExcel
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create an instance of Workbook class
Dim workbook As Workbook = New Workbook()
'Load an XML file
workbook.LoadFromXml("ToXML.xml")
'Save as XLSX file format
workbook.SaveToFile("ToExcel.xlsx", ExcelVersion.Version2016)
'Save as XLS file format
'workbook.SaveToFile("ToExcel.xls", ExcelVersion.Version97to2003);
End Sub
End Class
End Namespace
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2021/12/xmltoexcel.png?w=800)