Please disable your adblock and script blockers to view this page
 

How to bind GridView from Xml DataSource

Introduction: In the previous articles i explained How to read from XML file and bind to GridView in asp.net  and  How to sort DataSet and bind XML file data to GridView in asp.net.
Now In this article i am going to explain how to create xml file and  Bind/ Load/ Fill GridView data control from xml file in asp.net

Implementation: Let's understand by an example
  •  First Create a new website and add XML file. To add XML file follow the simple steps:
Go to Website Menu -> Add New Item -> Select XML File and give name “books.xml”. Then paste the following xml tags in the books.xml file
Create xml file as:

<?xml version="1.0" encoding="utf-8" ?>
<Books>
  <Book>
    <Title>ASP.NET 4.0</Title>   
    <Author>Ramanuj</Author>
    <Publisher>natraj publication</Publisher>
    <Price>800</Price>
  </Book>
  <Book>
    <Title>C#</Title>
    <Author>shiv prasad</Author>
    <Publisher>anand publication</Publisher>
    <Price>850</Price>
  </Book>
  <Book>
    <Title>Sql Server</Title>
    <Author>Yashwant</Author>
    <Publisher>daisy publication</Publisher>
    <Price>800</Price>  
  </Book>
</Books>
  • Place a gridview control on design page (.aspx)
<asp:GridView ID="grdBooks" runat="server">
</asp:GridView>

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

VB.Net code to bind GridView from Xml DataSource
  • In the code behind file(.aspx.vb) write the code as:
Protected Sub Page_Load(sender As Object, e As EventArgs)
                    readFromXMLShowInGrid() 
End Sub

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

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."

Previous
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.. ConversionConversion EmoticonEmoticon