How to read from XML file and bind to gridview in

Introduction: In the previous articles i explained How to bind GridView from Xml DataSource in and How to sort DataSet and bind XML file data to GridView in  Now In this article i will explain how to read data from XML file and insert that data into DataSet and then bind to GridView control.

  • Create xml file as:
<?xml version="1.0" encoding="utf-8" ?>
    <Title>ASP.NET 4.0</Title>   
    <Publisher>natraj publication</Publisher>
    <Author>shiv prasad</Author>
    <Publisher>anand publication</Publisher>
    <Title>Sql Server</Title>
    <Publisher>daisy publication</Publisher>

  •  Place a GridView control on design page (.aspx)
<asp:GridView ID="grdBooks" runat="server">

C#.Net Code to create XML file and read data and bind to GridView
  • In the code behind file(.aspx.cs) write the code as:
   protected void Page_Load(object sender, EventArgs e)
    private void readFromXMLShowInGrid()
        string myfile = @Server.MapPath("~/books.xml");
        DataSet ds = new DataSet();
        if (ds.Tables[0].Rows.Count > 0)
            grdBooks.DataSource = ds;
            Response.Write("No data to display");

VB.Net Code to create XML file and read data and bind to GridView
  • In the code behind file(.aspx.vb) write the code as:

Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub

Private Sub readFromXMLShowInGrid()
                    Dim myfile As String = Server.MapPath("~/books.xml")
                    Dim ds As New DataSet()
                    If ds.Tables(0).Rows.Count > 0 Then
                                         grdBooks.DataSource = ds
                                         Response.Write("No data to display")
                    End If
End Sub

Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..