This article will show you how to read xml file in dataset and
display in datagridview c#. In this I have a method to read the xml data and
save it into dataset.
Some of my previous articles are as follows: Read
XML File in DataTable and Bind to DataList In Asp.Net Using C#.Net, How
To Read XML File in DataTable Using C#.Net In Asp.Net MVC, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net MVC Using
C#.Net, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net Using C#.Net,
Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net, Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net,
Bind
Asp.net DropdownList Control by XML File Data Using DataSet in C#.Net, Bind
Asp.net ListBox Control by XML File Data Using DataSet in C#.Net, CheckBoxList
Bind By Using XmlDataSource in Asp.Net.
So for this article first we will create a new asp.net application and add an xml file into bib directory as shown below.
So for this article first we will create a new asp.net application and add an xml file into bib directory as shown below.
After this add the below code into the form cs file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void Form1_Load(object
sender, EventArgs e)
{
string
filepath = @"\XMLDataFile\XMLFile1.xml";
DataSet
objds = new DataSet();
objds = ReadXMLFile(filepath);
if
(objds.Tables[0].Rows.Count > 0)
{
dataGridView1.DataSource =
objds.Tables[0];
}
}
/// <summary>
/// Function to read xml data in datatable
/// how to read xml file in dataset in c#
/// </summary>
/// <param
name="filePath"></param>
/// <returns></returns>
private
DataSet ReadXMLFile(string filePath)
{
DataSet
objds = new DataSet();
string
fileFinalPath = AppDomain.CurrentDomain.BaseDirectory
+ filePath;
objds.ReadXml(fileFinalPath);
return
objds;
}
}
}
|
In above code I have used dataset xml reader to read the file at the given path and store it into dataset object. Now we have done run the application to check the output.
0 comments:
Please let me know your view