In this article I will explain with an example, how to export data from Database to XML file in ASP.Net Core MVC.
Note: For beginners in ASP.Net Core MVC, please refer my article ASP.Net MVC Core Hello World Tutorial with Sample Program example.
I have made use of the following table Customers with the schema as follows.
ASP.Net Core MVC: Export data from Database to XML File
I have already inserted few records in the table.
ASP.Net Core MVC: Export data from Database to XML File
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file
You will need to import the following namespaces.
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply View is returned.
Action method for handling POST operation
Inside this Action method, the DataSet is populated with records from the database table named Customers.
Next, the data fetched from the database is set to the DataSet.
Then, an instance of MemoryStream class is created.
Now, the DataSet is converted to an XML string using the WriteXml method and copied to the MemoryStream class object.
Finally, the MemoryStream class object is converted to Byte Array and downloaded as XML file using the File function.
public class HomeController : Controller
    public IActionResult Index()
        return View();
    public IActionResult Export()
        string constr = @"Data Source=.\SQL2019;Initial Catalog=Test; Integrated Security=true";
        using (SqlConnection con = new SqlConnection(constr))
           using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    using (DataSet ds = new DataSet("Customers"))
                        ds.Tables[0].TableName = "Customer";
                        using (MemoryStream stream = new MemoryStream())
                            return File(stream.ToArray(), "application/xml", "Customers.xml");
The View consists of an HTML Form which has been created using the following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the HTML Form there is a Submit Button.
Form Submitting
When the Export Button is clicked, the form is submitted and the XML file is downloaded.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <form method="post" asp-controller="Home" asp-action="Export">
        <input type="submit" value="Export" />
The Form
ASP.Net Core MVC: Export data from Database to XML File
The generated XML file
s ASP.Net Core MVC: Export data from Database to XML File