Hi amar,
Check this example. Now please take its reference and correct your code.
XML
<?xml version="1.0" encoding="utf-8" ?>
<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
public class CustomerModel
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Namespaces
using System.Collections.Generic;
using System.Xml;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
settings.ProhibitDtd = false;
XmlReader xmlReader = XmlReader.Create(Server.MapPath("~/Customers.xml"), settings);
XmlDocument doc = new XmlDocument();
doc.Load(xmlReader);
List<CustomerModel> customers = new List<CustomerModel>();
foreach (XmlNode node in doc.SelectNodes("/Customers/Customer"))
{
customers.Add(new CustomerModel
{
CustomerId = int.Parse(node["Id"].InnerText),
Name = node["Name"].InnerText,
Country = node["Country"].InnerText
});
}
return View(customers);
}
}
View
@using XmlReaderSettings_MVC.Models
@model IEnumerable<CustomerModel>
@{
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 customer in Model)
{
<tr>
<td>@customer.CustomerId</td>
<td>@customer.Name</td>
<td>@customer.Country</td>
</tr>
}
</table>
</body>
</html>
Screenshot