Wednesday 16 January 2013

Read Xml file in GridView in asp.net using c#.


In this post I will explain how to read XML file in GridView in asp.net using c#.

Description:-

I have written many post regarding gridView . In this post I will explain how to read a xml file in griview or how to bind xml data to gridview. For this first Create an xml file and put some dummy data in it.
Create a xml file name Employee.xml and insert some data in this file.

Employee.xml:-

<?xml version="1.0" encoding="utf-8" ?>
<employees>
  <employee>
    <EmpName>Sushil Kumar</EmpName>
    <EmailId>Sushilct25@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>9998882345</PhoneNo>
  </employee>
  <employee>
    <EmpName>Prdeep Kumar</EmpName>
    <EmailId>pradeep@gmail.com</EmailId>
    <Designation>Manager</Designation>
    <PhoneNo>12344567890</PhoneNo>
  </employee>
  <employee>
    <EmpName>sushil Modi</EmpName>
    <EmailId>modi@gmail.com</EmailId>
    <Designation>Soft. Developer</Designation>
    <PhoneNo>224525426</PhoneNo>
  </employee>
  <employee>
    <EmpName>Rahul Ranjan</EmpName>
    <EmailId>ranjan@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>1245225</PhoneNo>
  </employee>
  <employee>
    <EmpName>Chulbul Pandey</EmpName>
    <EmailId>pandey@gmail.com</EmailId>
    <Designation>Economist</Designation>
    <PhoneNo>235235353</PhoneNo>
  </employee>
</employees>

After that add a web page to the project and drag  a GidView on this page like this..

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Read Xml File In GridView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84"
            BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
            CellSpacing="2">
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

After that add the following namespace  in codebehind:-

using System.Data;
using System.Xml;

Code behind page:-
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
    public void BindGridView()
    {
        XmlTextReader xmlReader = new XmlTextReader(Server.MapPath("Employee.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlReader);
        xmlReader.Close();
        if (ds.Tables.Count != 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columnCount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "No Record Found....";
        }
    }

Output  like this:-


No comments:

Post a Comment

Note: only a member of this blog may post a comment.