In this article I will explain with an example, how to display XML file data in .Net Core.
This article will make use of ASP.Net MVC Core for illustrating how to read XML file data and then display it in Grid (HTML Table) format in .Net Core.
Note: For beginners in ASP.Net MVC Core, please refer my article ASP.Net MVC Core Hello World Tutorial with Sample Program example.
 
 
XML File
The following is the XML file consisting of records of Customers. The XML file is saved with the name Customers.xml inside the wwwroot folder.
<?xml version="1.0" standalone="yes"?>
<Customers>
  <Customer>
    <Id>1</Id>
    <Name>John Hammond</Name>
    <Country>United States</Country>
  </Customer>
  <Customer>
    <Id>2</Id>
    <Name>Mudassar Khan</Name>
    <Country>India</Country>
  </Customer>
  <Customer>
    <Id>3</Id>
    <Name>Suzanne Mathews</Name>
    <Country>France</Country>
  </Customer>
  <Customer>
    <Id>4</Id>
    <Name>Robert Schidner</Name>
    <Country>Russia</Country>
  </Customer>
</Customers>
 
 
Namespaces
You will need to import the following namespaces.
using System.Xml;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
 
 
Model
Following is a Model class named CustomerModel with three properties i.e. CustomerId, Name and Country.
public class CustomerModel
{
    ///<summary>
    /// Gets or sets CustomerId.
    ///</summary>
    public int CustomerId { get; set; }
 
    ///<summary>
    /// Gets or sets Name.
    ///</summary>
    public string Name { get; set; }
 
    ///<summary>
    /// Gets or sets Country.
    ///</summary>
    public string Country { get; set; }
}
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside the Index Action method, the XML file is read from the wwwroot folder using IHostingEnvironment interface.
Note: The IHostingEnvironment interface is injected using Dependency Injection inside the Controller class, for more details please refer Using IHostingEnvironment in ASP.Net Core.
 
The XML file data is loaded into an XmlDocument class object.
Then the Customer Node is selected using XPath query and a 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 Model class object and a Generic list collection of Model class objects is prepared.
Finally, the Generic list collection of Model class objects is returned to the View.
public class HomeController : Controller
{
    private IHostingEnvironment Environment;
 
    public HomeController(IHostingEnvironment _environment)
    {
        this.Environment = _environment;
    }
 
    public IActionResult Index()
    {
        List<CustomerModel> customers = new List<CustomerModel>();
 
        //Load the XML file in XmlDocument.
        XmlDocument doc = new XmlDocument();
        doc.Load(string.Concat(this.Environment.WebRootPath, "/Customers.xml"));
 
        //Loop through the selected Nodes.
        foreach (XmlNode node in doc.SelectNodes("/Customers/Customer"))
        {
            //Fetch the Node values and assign it to Model.
            customers.Add(new CustomerModel
            {
                CustomerId = int.Parse(node["Id"].InnerText),
                Name = node["Name"].InnerText,
                Country = node["Country"].InnerText
            });
        }
 
        return View(customers);
    }
}
 
 
View
Inside the View, the Customer Model class is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@using Display_XML_MVC_Core.Models
@model IEnumerable<CustomerModel>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>CustomerId</th>
            <th>Name</th>
            <th>Country</th>
        </tr>
        @foreach (CustomerModel customer in Model)
        {
            <tr>
                <td>@customer.CustomerId</td>
                <td>@customer.Name</td>
                <td>@customer.Country</td>
            </tr>
        }
    </table>
</body>
</html>
 
 
Screenshot
Display XML File data in .Net Core
 
 
Downloads