This article will show you how you can read and xml file inasp.net mvc using c#.Net by using dataset and datatable.
Some of my previous articles are as follows: Search and Display Data In MVC
WebGrid in Asp.Net MVC Using C#.Net, Cookies
Create, Read and Delete Operation Using jQuery in Asp.Net, MVC
Pass Multiple Parameters in QueryString or URL in Asp.Net MVC, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net MVC Using
C#.Net, jQuery
Ajax Search and Display In MVC WebGrid in Asp.Net MVC Using C#.Net, Search
and Display Data In MVC WebGrid in Asp.Net MVC Using C#.Net, Bind
WebGrid With Entity Framework in Asp.Net MVC Using C#.Net, Asp.Net
MVC Export Data to Excel File Of WebGrid Using C#.Net.
So for this article first we will create a new asp.net mvc application and add an xml folder. In this folder we will add an xml file.
So for this article first we will create a new asp.net mvc application and add an xml folder. In this folder we will add an xml file.
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.
0 comments:
Please let me know your view