dev-exchange.com logo

dev-exchange.com latest topics RSS feed

Book mark: Add http://www.dev-exchange.com/cms_view_article.php?aid=25&start=0&sid=b362fcf84c8ee0980c9fd468163500bb to your delicious account Add http://www.dev-exchange.com/cms_view_article.php?aid=25&start=0&sid=b362fcf84c8ee0980c9fd468163500bb to your digg account Add http://www.dev-exchange.com/cms_view_article.php?aid=25&start=0&sid=b362fcf84c8ee0980c9fd468163500bb to your blinklist account Add http://www.dev-exchange.com/cms_view_article.php?aid=25&start=0&sid=b362fcf84c8ee0980c9fd468163500bb to your reddit account Add http://www.dev-exchange.com/cms_view_article.php?aid=25&start=0&sid=b362fcf84c8ee0980c9fd468163500bb to Dzone Add http://www.dev-exchange.com/cms_view_article.php?aid=25&start=0&sid=b362fcf84c8ee0980c9fd468163500bb to your furl account Add http://www.dev-exchange.com/cms_view_article.php?aid=25&start=0&sid=b362fcf84c8ee0980c9fd468163500bb to your stumble account Add http://www.dev-exchange.com/cms_view_article.php?aid=25&start=0&sid=b362fcf84c8ee0980c9fd468163500bb to your Yahoo myweb account
Article Menu
Google
ColdFusion MX
PHP
ASP
ASP.Net
XML-XSLT
JavaScript
SQL Server
AJAX
Android
 


 
Article
MsN
Tech Author

Articles: 7 Comments: 1
 Posted: Fri Feb 01, 2008 2:55 pm

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

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.
Rating: 0.00/5.00 [0]

Comments
krazik
Author


Joined: 22 Aug 2008
Articles: 0
Comments: 1
 Posted: Fri Aug 22, 2008 5:29 pm  Post subject:

Just wanted to thank you for the example. This helped me a lot.

-Ry
Rating: 0.00/5.00 [0]
 

Page 1 of 1
All times are GMT

Jump to:  
You cannot post articles in this chapter
You cannot edit your articles in this chapter
You cannot delete your articles in this chapter
You cannot rate articles in this chapter

You cannot post comments in this chapter
You cannot edit your comments in this chapter
You cannot delete your comments in this chapter
You cannot rate comments in this chapter


© 2008 dev-exchange.com
Powered by phpBB. Theme DEVPPL.