How to bind xml with grid view in asp.net c#

In this article, I will bind the gridview with xml file. Xml file is client side mark up language so its my very good way to bind the data from client side
XML File :-


<?xml version="1.0" encoding="utf-8" ?>
<EmployeeName>
  <empname>
    <id>1</id>
    <name>Neeraj Srivastava</name>
  </empname>
  <empname>
    <id>2</id>
    <name>Sanjay kumar</name>
  </empname>
  <empname>
    <id>3</id>
    <name>Arun Kumar</name>
  </empname>
  <empname>
    <id>4</id>
    <name>Meenakshi Pandey</name>
  </empname>
</EmployeeName>

Source Code:-

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

<!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 align="center">
        <fieldset style="width: 250px;">
            <legend>XML Bind with Gridview</legend>
            <asp:GridView ID="gdemp" runat="server">


            </asp:GridView>
        </fieldset>
    </div>
    </form>
</body>
</html>


Code behind:-

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 bindxml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindddlXml();


    }
    private void BindddlXml()
    {
           
        using (DataSet ds = new DataSet())
        {
           
            ds.ReadXml(Server.MapPath("~/Empname.xml"));
            gdemp.DataSource = ds;
            gdemp.DataBind();
        }
    }
}


 Out Put:-


How to bind xml with grid view in asp.net c# How to bind xml with grid view in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 8:59:00 PM Rating: 5

No comments:

Powered by Blogger.