Friday, November 26, 2010

How to bind XML File or XML DataSource to a DataList Control in Asp.Net

Please visit my new Web Site

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:

Bind XML data into DataList controlo

To do the above example we need to write an XML file like:
<?xml version="1.0" encoding="utf-8" ?>
    <Name>Shawpnendu Bikash Maloroy</Name>
    <Address>Uttara, Dhaka</Address>
    <Name>Bimolandu Bikash Maloroy</Name>
    <Address>Sonaimuri, Noakhali</Address>
    <Name>Purnendu Bikash Maloroy</Name>
    <Address>FirmGate, Dhaka</Address>
    <Name>Shadesh Chandra Chanda</Name>
    <Address>Maijdee, Noakhali</Address>
    <Name>Sajal Chandra Chanda</Name>
    <Address>Maijdee, Noakhali</Address>

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" "">

<html xmlns="">
<head runat="server">
    <title>An Example of Binding XML Datasource into DataList Control</title>
    <form id="form1" runat="server">
    <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1"> 
        <HeaderTemplate>Customer Name</HeaderTemplate>
        <ItemStyle BackColor="Gray" ForeColor="Yellow" />
        <AlternatingItemStyle BackColor="Silver" />
            <%# XPath("Name")%>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" 

Now run the page. Hope you will get your desired output.


RAM said...

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.

Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash