Please visit my new Web Site https://coderstechzone.com
DataList control is an important control in Asp.Net applications. Most of the times we need to bind DataList control from a Database using Ado.Net datasource. But sometimes we need to bind XML file as Datasource into a DataList control. Here in this article i will demonstrate how one can bind XML data into a DataList control using the datasource XmlDataSource. The output will be:
To do the above example we need to write an XML file like:
<?xml version="1.0" encoding="utf-8" ?> <Customers> <Customer> <Name>Shawpnendu Bikash Maloroy</Name> <Address>Uttara, Dhaka</Address> <City>Dhaka</City> <Phone>011789657</Phone> </Customer> <Customer> <Name>Bimolandu Bikash Maloroy</Name> <Address>Sonaimuri, Noakhali</Address> <City>Noakhali</City> <Phone>019789687</Phone> </Customer> <Customer> <Name>Purnendu Bikash Maloroy</Name> <Address>FirmGate, Dhaka</Address> <City>Dhaka</City> <Phone>018788767</Phone> </Customer> <Customer> <Name>Shadesh Chandra Chanda</Name> <Address>Maijdee, Noakhali</Address> <City>Noakhali</City> <Phone>015787597</Phone> </Customer> <Customer> <Name>Sajal Chandra Chanda</Name> <Address>Maijdee, Noakhali</Address> <City>Noakhali</City> <Phone>019734557</Phone> </Customer> </Customers>
Now add an aspx page into your project & modify the HTML markup like below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList_XML.aspx.cs" Inherits="DataList_XML" %> <!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>An Example of Binding XML Datasource into DataList Control</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1"> <HeaderTemplate>Customer Name</HeaderTemplate> <ItemStyle BackColor="Gray" ForeColor="Yellow" /> <AlternatingItemStyle BackColor="Silver" /> <ItemTemplate> <%# XPath("Name")%> </ItemTemplate> </asp:DataList> <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="Customers.xml" XPath="//Customers/Customer"> </asp:XmlDataSource> </div> </form> </body> </html>
Now run the page. Hope you will get your desired output.
1 comments:
Its great example and excellent work for binding xml file to any datagrid or datlist,its meeting some word my requirement as well, but in my case i am getting the output from wcf restservice which is in same format xml file content but its not physically storing in any location, can you please help me out to store the xml format output to any resource then i can bind that data to datagrid or datalist.
I WOULD BE DELIGHTED TO HEAR FROM YOU