Tuesday, May 26, 2009

How to bind a XML file to a GridView using Asp.net C#




Please visit my new Web Site WWW.Codedisplay.com



Recently one of the most popular data source is XML. For simplicity developer wants to make an XML file since its easy to use in the Asp.net built in data controls like GridView. In this post i will try to show you how one can bind easily XML file as a data source of GridView control. In the later section i will discuss how to bind XML file to a GridView control in run time.

Focus Area:
1. Bind XML file in design mode
2. Bind XML file programetically


Bind XML file in design mode:
To run this example at first add an XML file in your project & keep the name XMLFile.xml. Now modify the XML in the following way:

<?xml version="1.0" encoding="utf-8" ?>
<
Article>
<
Asp.net Id="0001" Title="Bind Dropdown List" Visit="985" Modified="Jan 01, 2009">
</
Asp.net>
<
Asp.net Id="0002" Title="Event calendar" Visit="777" Modified="Feb 01, 2009">
</
Asp.net>
<
Asp.net Id="0003" Title="Select all checkboxes" Visit="853" Modified="Mar 01, 2009">
</
Asp.net>
<
Asp.net Id="0004" Title="Transfer data between" Visit="957" Modified="Apr 01, 2009">
</
Asp.net>
</
Article>

Now add a GridView control in your page. The next step is to click the GridView control's smart tag and select from the Choose Data Source dropdown list. This opens the Data Source Configuration Wizard, as shown in below:



Now click on browse button to set a Data File. Bydefault you found that our previously cretaed XMLFile.xml is selected. So just click OK--OK. Now run the project. Hope you will get the output page like below:



In Configuration data source wizard you saw that i keep blank Transform file & Xpath expression. You can set the Transform file value by your XSLT & use Xpath expression to bind the selected node from your XML file.

To bind an XML file runtime into a GridView using Asp.net you can follow the below code:
protected void Page_Load(object sender, EventArgs e)
{
DataSet XMLDataSet;
string FilePath = Server.MapPath("Your XML File HERE");
XMLDataSet = new DataSet();

//Read the contents of the XML file into the DataSet
XMLDataSet.ReadXml(FilePath);
GridView1.DataSource = XMLDataSet.Tables[0].DefaultView;
GridView1.DataBind();
}

Happy programming.

1 comments:

Pravesh Singh said...

Very informative post. It's really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about Populate Grid Control From XML Document Easily, for more details of this post check out this link...

http://mindstick.com/Articles/6c3ccf8c-6656-4a48-bfb7-e0221569de67/?Populate%20Grid%20Control%20From%20XML%20Document%20Easily

Thanks Everyone!!

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash