In this article I will explain with an example, how to read (parse) XML file in ASP.Net Core (.Net Core) MVC.
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 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 { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Namespaces
You will need to import the following namespace.
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.
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
Downloads