In this article I will explain with an example, how to read (parse) XML file in ASP.Net Core (.Net Core) MVC.
Note: For beginners in ASP.Net Core (.Net Core 7), please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

XML File Location

The XML file is saved with the name Customers inside the XML Folder (Directory) of wwwroot Folder (Directory).
Read (Parse) XML file in ASP.Net Core MVC
 
 

XML File

The following XML file will be used in this article consisting of Customer records.
<?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>
 
 

Model

The Model class consists of following properties.
public class CustomerModel
{
    public int CustomerId { getset; }
    public string Name { getset; }
    public string Country { getset; }
}
 
 

Namespaces

You will need to import the following namespace.
using System.Xml;
 
 

Controller

The Controller consists of following Action method.

Action method for handling GET operation

Inside the Action method, first a Generic List collection of CustomerModel class object is created.
Then, the XmlDocument class object is created and XML file is read from the wwwroot folder using IWebHostingEnvironment interface and XML file data is loaded into an XmlDocument class object.
Note: The IWebHostEnvironment interface is injected using Dependency Injection inside the IndexModel class, for more details please refer my article Using IWebHostEnvironment in ASP.Net Core. For more details on how to access Static Files in ASP.Net Core, please refer my article Static Files (Images, CSS and JS files) in ASP.Net Core.
 
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 properties of the CustomerModel class object.
Finally, the Generic List collection of CustomerModel class object is returned to the View.
public class HomeController : Controller
{
    private IWebHostEnvironment Environment;
    public HomeController(IWebHostEnvironment _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, "/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.
            customers.Add(new CustomerModel
            {
                 CustomerId = int.Parse(node["Id"].InnerText),
                 Name = node["Name"].InnerText,
                 Country = node["Country"].InnerText
            });
        }
        return View(customers);
    }
}
 
 

View

HTML Markup

Inside the View, the CustomerModel class is declared as IEnumerable which specifies that it will be available as a Collection.
The View also 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 Customers records which is read from the Customers.xml file.
@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>Customer Id</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

Read (Parse) XML file in ASP.Net Core MVC
 
 

Downloads