Read XML file and display data using MVC | ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

Read XML file and display data using MVC

In this article I am going to explain how to read XML file and display data using MVC.

I have XML of student detail. I want to read this file and display its data.

XML File:
<?xml version="1.0"?>


First of all I have created a class in model.
public partial class Studentdetail
        public int ID { get; set; }
        public string name { get; set; }
        public string class_Section { get; set; }
          public string roll_no { get; set; }

Add controller
Add empty controller to project. In index action method write the code to read xml file and display data.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_Project.Models;
using System.Xml;

namespace MVC_Project.Controllers
    public class defaultController : Controller
        public ActionResult Index()
            XmlDocument doc = new XmlDocument();
            List<Studentdetail> students = new List<Studentdetail>();
            foreach (XmlNode node in doc.SelectNodes("/students/student"))
                students.Add(new Studentdetail
                    name = node["name"].InnerText,
                    class_Section = node["class_Section"].InnerText,
                    roll_no = node["roll_no"].InnerText
            return View(students);
Add view
Add view for index action.

    ViewBag.Title = "Index";

    table tr td{padding:0 20px}

        <th>Class section</th>
        <th>Roll no.</th>
    @foreach (MVC_Project.Models.Studentdetail student in Model)

Post a Comment



Contact Form


Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget