In this article I will explain with an example, how to export data from Database to XML file in ASP.Net Core Razor Pages.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Namespaces
You will need to import the following namespaces.
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Razor PageModel (Code-Behind)
The Controller consists of following Handler methods.
Handler method for handling GET operation
This Handler method is left empty as it is not required.
Handler method for handling File Export operation
This Handler method handles the File Download operation and when the Export Button is clicked.
Note: The following Handler method performs File Download and hence the return type is set to FileResult.
Inside this Handler 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 IndexModel : PageModel
{
public void OnGet()
{
}
public FileResult OnPostExport()
{
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"))
{
sda.Fill(ds);
ds.Tables[0].TableName = "Customer";
using (MemoryStream stream = new MemoryStream())
{
ds.WriteXml(stream);
return File(stream.ToArray(), "application/xml", "Customers.xml");
}
}
}
}
}
}
}
Razor Page (HTML)
The View consists of an HTML Form which has been created using the following TagHelpers attribute.
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.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostExport but here it will be specified as Export when calling from the Razor HTML Page.
Form Submitting
When the Export Button is clicked, the form is submitted and the XML file is downloaded.
@page
@model Export_Data_Database_XML_Core_Razor.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post">
<input type="submit" value="Export" asp-page-handler="Export" />
</form>
</body>
</html>
Screenshots
The Form
The generated XML file
Demo
Downloads