.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Bind a Xml file data to Gridview

Posted By:Sanjeeb Lenka       Posted Date: November 26, 2013    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article i will show how to bind a Xml file data to Grid view in Asp.net.
 

In this article i will show how to bind a Xml file data to Grid view in Asp.net.

First create a a xml file like following file:

<?xml version="1.0" encoding="utf-8" ?>
<data>
	<Employees>
		<ID>1</ID>
		<FirstName>sanjeeb</FirstName>
		<LastName>Lenka</LastName>
	</Employees>
	<Employees>
		<ID>1</ID>
		<FirstName>surezh</FirstName>
		<LastName>broad</LastName>
	</Employees>
</data>
Add a page to the website and add a grid to the page .

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>       
    </div>
    </form>
</body>
</html>

then write the following code in .cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/XMLFile.xml"));
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();

    }
   
}
now build the website . it will bind the xml file data to gridview..

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend