Saturday, 15 January 2022

XML File Read(Parse) and Display In Table/Tabular Format in ASP.Net Core 6 / MVC Using C#.Net

1/15/2022 - By Pranav Singh 0

 In this article i will show you how you can read or parse XML file and display xml file data table or  tabular format in your asp.net core 6/MVC application using c#.net. 

Other Articles: Asp.Net Core 6 : Rename / Change File or Image Name and Upload in wwwroot Using C#.NetFile Upload in Folder (Folder Outside wwwroot) Asp.Net Core 6 / MVC Using C#Access Connection String In Asp.Net Core 6 Controller From appsettings.json C#How to get value from XML using Linq in C#.

Now for this article we will create a new asp.net core 6 application and add and XML file in your  xml file  folder and add your xml file detail.


<?xml version="1.0" encoding="utf-8" ?>

<students>

  <student>

    <rollno>1</rollno>

    <name>Student Name 1</name>

  </student>

  <student>

    <rollno>2</rollno>

    <name>Student Name 2</name>

  </student>

  <student>

    <rollno>3</rollno>

    <name>Student Name 3</name>

  </student>

</students>


After this we will open our controller and add code to read the xml file and it's node values in a tabular format. Now we will create a model class and add the below code it.

public class StudentModel

    {

        public List<StudentDetail> StudentDetailList { get; set; }

    }

    public class StudentDetail

    {

        public int RollNo { get; set; }

        public string StudentName { get; set; }

    }


After creating model class we will add code to our controller to read the xml file and display it in tabular format. 

[HttpGet]

        public IActionResult Index()

        {

            StudentModel studentModel = new StudentModel();

            studentModel.StudentDetailList = new List<StudentDetail>(); 

            string xmlfilepath = Path.Combine(Directory.GetCurrentDirectory(), "XMLFiles", "StudentXML.xml");

            var xmlString = System.IO.File.ReadAllText(xmlfilepath);

            var stringReader = new StringReader(xmlString);

            var dsSet = new System.Data.DataSet();

            dsSet.ReadXml(stringReader); 

            for (int i = 0; i < dsSet.Tables[0].Rows.Count; i++)

            {

                studentModel.StudentDetailList.Add(new StudentDetail

                {

                    RollNo = (int)dsSet.Tables[0].Rows[i][0],

                    StudentName = dsSet.Tables[0].Rows[i][1].ToString()

                });

            }          

            return View(studentModel);

        }


In above code first i have crate the object of model class. After that i have prepared the path of the xml file. and read it in a dataset.  Now here is the xml file data into the dataset.

read xml in dataset in in asp.net core

After reading data into dataset i have added value to list model and pass it to view. Now we will write code to display data in view.

@model Project.Models.StudentModel

@{

    ViewData["Title"] = "Home Page";

}

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "formxmldata", @enctype = "multipart/form-data" }))

{

    <table class="table">

        <thead>

            <tr>

                <th>Roll No</th>

                <th>Student Name</th>

            </tr>

        </thead>

        <tbody>

            @foreach (var item in Model.StudentDetailList)

            {

                <tr>

                    <td>@item.RollNo</td>

                    <td>@item.StudentName</td>

                </tr>

            }

        </tbody>

    </table>

}

 


In above code i have bind the data to the table control.

xml data display in table in asp.net core
Download

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top