In this article I will explain with an example, how to read (parse) XML file in ASP.Net Core Razor Pages.
XML File Location
The XML file is saved with the name Customers inside the XML Folder (Directory) of wwwroot Folder (Directory).
XML File
The following XML file will be used in this article consisting of records of Customers.
<?xml version="1.0" standalone="yes"?>
<Customers>
<Customer>
<CustomerId>1</CustomerId>
<Name>John Hammond</Name>
<Country>United States</Country>
</Customer>
<Customer>
<CustomerId>2</CustomerId>
<Name>Mudassar Khan</Name>
<Country>India</Country>
</Customer>
<Customer>
<CustomerId>3</CustomerId>
<Name>Suzanne Mathews</Name>
<Country>France</Country>
</Customer>
<Customer>
<CustomerId>4</CustomerId>
<Name>Robert Schidner</Name>
<Country>Russia</Country>
</Customer>
</Customers>
Model
The Model class consists of following properties.
public class CustomerModel
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Namespaces
You will need to import the following namespace.
Razor PageModel(Code-Behind)
The PageModel consists of following Handler method.
Handler method for handling GET operation
Inside this Handler method, first the Generic List of CustomerModel class is created.
Then, the XML file is read from the wwwroot folder using IWebHostingEnvironment interface and XML file data is loaded into an XmlDocument class object.
Then, the Customer Node is selected using XPath query and a FOR EACH loop is executed over all the selected Nodes.
Inside the loop, the values are extracted from each Child Node and assigned to appropriate property of the CustomerModel class object.
public class IndexModel : PageModel
{
private IWebHostEnvironment Environment;
public IndexModel(IWebHostEnvironment _environment)
{
this.Environment = _environment;
}
public List<CustomerModel> Customers { get; set; }
public void OnGet()
{
this.Customers = new List<CustomerModel>();
//Load the XML file in XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load(string.Concat(this.Environment.WebRootPath, "/XML/Customers.xml"));
//Loop through the selected Nodes.
foreach (XmlNode node in doc.SelectNodes("/Customers/Customer"))
{
//Fetch the Node values and assign it to Model.
this.Customers.Add(new CustomerModel
{
CustomerId = int.Parse(node["CustomerId"].InnerText),
Name = node["Name"].InnerText,
Country = node["Country"].InnerText
});
}
}
}
Razor Page (HTML)
The Razor Page consists of an HTML Table for displaying the records.
A FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the Customer records read from the Customers.xml file.
@page
@model Read_XML_Core_Razor.Pages.IndexModel
@using Read_XML_Core_Razor.Models
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table>
<tr>
<th>Customer Id</th>
<th>Name</th>
<th>Country</th>
</tr>
@foreach (CustomerModel customers in Model.Customers)
{
<tr>
<td>@customers.CustomerId</td>
<td>@customers.Name</td>
<td>@customers.Country</td>
</tr>
}
</table>
</body>
</html>
Screenshot
Demo
Downloads