Skip to main content

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

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:
01<?xml version="1.0" encoding="utf-8" ?>
02<Customers>
03  <Customer>
04    <Name>Shawpnendu Bikash Maloroy</Name>
05    <Address>Uttara, Dhaka</Address>
06    <City>Dhaka</City>
07    <Phone>011789657</Phone>
08  </Customer>
09  <Customer>
10    <Name>Bimolandu Bikash Maloroy</Name>
11    <Address>Sonaimuri, Noakhali</Address>
12    <City>Noakhali</City>
13    <Phone>019789687</Phone>
14  </Customer>
15  <Customer>
16    <Name>Purnendu Bikash Maloroy</Name>
17    <Address>FirmGate, Dhaka</Address>
18    <City>Dhaka</City>
19    <Phone>018788767</Phone>
20  </Customer>
21  <Customer>
22    <Name>Shadesh Chandra Chanda</Name>
23    <Address>Maijdee, Noakhali</Address>
24    <City>Noakhali</City>
25    <Phone>015787597</Phone>
26  </Customer>
27  <Customer>
28    <Name>Sajal Chandra Chanda</Name>
29    <Address>Maijdee, Noakhali</Address>
30    <City>Noakhali</City>
31    <Phone>019734557</Phone>
32  </Customer>
33</Customers>

Now add an aspx page into your project & modify the HTML markup like below:
01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList_XML.aspx.cs" Inherits="DataList_XML" %>
02 
03<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 
05<html xmlns="http://www.w3.org/1999/xhtml">
06<head runat="server">
07    <title>An Example of Binding XML Datasource into DataList Control</title>
08</head>
09<body>
10    <form id="form1" runat="server">
11    <div>
12     
13    <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1">
14        <HeaderTemplate>Customer Name</HeaderTemplate>
15        <ItemStyle BackColor="Gray" ForeColor="Yellow" />
16        <AlternatingItemStyle BackColor="Silver" />
17        <ItemTemplate>
18            <%# XPath("Name")%>
19        </ItemTemplate>
20    </asp:DataList>
21     
22    <asp:XmlDataSource ID="XmlDataSource1" runat="server"
23        DataFile="Customers.xml"
24        XPath="//Customers/Customer">
25    </asp:XmlDataSource>
26     
27    </div>
28    </form>
29</body>
30</html>

Comments

Popular posts from this blog

Editing Child GridView in Nested GridView

Editing Child GridView in Nested GridView In this article we will explore how to edit child gridview in the nested gridview.   Let''s write some code. Step 1:  Add scriptmanager in the aspx page. < asp : ScriptManager   ID ="ScriptManager1"   runat ="server"   EnablePageMethods ="true"> </ asp : ScriptManager > Step 2:  Add below stylesheet for modal popup. < style   type ="text/css">        .modalBackground        {              background-color : Gray;              filter : alpha(opacity=80);              opacity : 0.5;       }        .ModalWindow        {              border : solid1px#c0c0c0;              background : #f0f0f0;              padding : 0px10px10px10px;              position : absolute;              top : -1000px;       } </ style > Step 3:   Create an aspx page and add a Gridview with another gridview in the last TemplateField. The last templatefield will also contain a lable which will

Scrollable Gridview With fixheader using JQuery in Asp.net

Scrollable Gridview With fixheader using JQuery in Asp.net Introduction: In this article I will explain how to implement scrollable gridview with fixed header in asp.net using JQuery.  Description:  In Previous posts I explained lot of articles regarding Gridview. Now I will explain how to implement scrollable gridview with fixed header in asp.net. I have one gridview that contains lot of records and I used  paging for gridview  but the requirement is to display all the records without paging. I removed paging at that time gridview occupied lot of space because it contains more records to solve this problem we implemented scrollbar.  After scrollbar implementation if we scroll the gridview we are unable to see Gridview header.   To implement Scrollable gridview with fixed header I tried to implement concept with css and JavaScript but there is no luck because maintaining fixed header working in IE but not in Mozilla and vice versa to solve this browser compatibility proble

c# list multiple data types

 public class New_Library         {             public string File_Name { get; private set; }             public string File_Full_Path { get; private set; }             public string checksum { get; private set; }             public New_Library(string File_Name1, string File_Full_Path1, string checksum1)             {                 File_Name = File_Name1;                 File_Full_Path = File_Full_Path1;                 checksum = checksum1;             }         }         private void Form1_Load(object sender, EventArgs e)         {             List<New_Library> list_New_Library = new List<New_Library>();             list_New_Library.Add(new New_Library("sometext", "Vishal", "123456")); // store text             list_New_Library.Add(new New_Library("sometext1", "Vishal1", "1234561")); // store text             // read values from List             foreach (New_Library slot in list_New_Libr