Monday, 3 August 2015

How To Read XML File in DataTable Using C#.Net In Asp.Net MVC

8/03/2015 - By Pranav Singh 0

This article will show you how you can read and xml file inasp.net mvc using c#.Net by using dataset and datatable.




Now add some value to this xml file.

<?xml version="1.0" encoding="utf-8" ?>
<students>
  <student>
    <id>1</id>
    <name>Student 1</name>
    <address>Address 1</address>
  </student>
  <student>
    <id>2</id>
    <name>Student 2</name>
    <address>Address 2</address>
  </student>
  <student>
    <id>1</id>
    <name>Student 3</name>
    <address>Address 3</address>
  </student>
  <student>
    <id>1</id>
    <name>Student 4</name>
    <address>Address 4</address>
  </student>
</students>

Above is the student record xml file. Now we will add an xml controller file, in your controller add the below code.

using System.Web.Mvc;
using System.Data;

namespace XMLReadeinMVC.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            DataTable objDataTable = new DataTable();
            string filepathe = @"XMLFile\XMLFile1.xml";
            objDataTable = ReadXMLFile(filepathe);
            return View();
        }
        /// <summary>
        /// Function to read xml data in datatable
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private DataTable ReadXMLFile(string filePath)
        {
            DataSet objds = new DataSet();
            objds.ReadXml(Server.MapPath(filePath));
            return objds.Tables[0];
        }
    }
}
In above code I have created a method which will return the data as datatable. This method just need the xml file path. Now we have done run the application to check the output. For checking the output just put a break point and after hitting break point just view the dataset.


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