Binding XML data to Gridview Control
Here is an example how easily we can bind the XML data to GridView control using XML data source.
I have a XML file called people.xml in APP_DATA folder which i like to display it on asp.net page. There are plenty of ways you can do this. For example write a XSL file and transform it but for showing you the Gridview binding i'm taking this route.
people.xml
Now i got to place a GridView control in asp.net page along with XMLDatasource.
In XML datasource control i'm calling XML Data file and asking control to select the nodelist /PEOPLE/PERSON using XPATH query.
That is it when you run your page it should display the XML contents on GridView control.
Please post your comments on this article.
I have a XML file called people.xml in APP_DATA folder which i like to display it on asp.net page. There are plenty of ways you can do this. For example write a XSL file and transform it but for showing you the Gridview binding i'm taking this route.
people.xml
| Code: |
| <?xml version="1.0" ?>
<PEOPLE> <PERSON> <NAME>Mr James Antony</NAME> <ADDRESS>123, Mount Street, London</ADDRESS> <EMAIL>Test@someurlll.com</EMAIL> </PERSON> <PERSON> <NAME>Dr Phip Nat</NAME> <ADDRESS>878 ABV NewYork</ADDRESS> <EMAIL>Test1@someurlll.com</EMAIL> </PERSON> <PERSON> <NAME>Prof K KILIJ</NAME> <ADDRESS>2343 joik THAI</ADDRESS> <EMAIL>Test2@someurlll.com</EMAIL> </PERSON> </PEOPLE> |
Now i got to place a GridView control in asp.net page along with XMLDatasource.
| Code: |
| <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>XML Data to GridView</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="gwPeopleData" DataSourceID="XMLD" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label runat="server" ID="lblNAME" Text='<%#XPath("NAME")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <ItemTemplate> <asp:Label runat="server" ID="lblNAME" Text='<%#XPath("ADDRESS")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:Label runat="server" ID="lblEmail" Text='<%#XPath("EMAIL")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:XmlDataSource ID="XmlD" runat="server" DataFile="~/App_Data/people.xml" XPath="/PEOPLE/PERSON"> </asp:XmlDataSource> </div> </form> </body> </html> |
In XML datasource control i'm calling XML Data file and asking control to select the nodelist /PEOPLE/PERSON using XPATH query.
That is it when you run your page it should display the XML contents on GridView control.
Please post your comments on this article.
Commentaires
Enregistrer un commentaire