Load Bind XML File Data into Datalist Asp.Net C# VB.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 a Datasource into a DataList control. Here in this article i will demonstrate how one can bind or load XML data into a DataList control using the datasource XmlDataSource.

To do that first add a XML file in your project. Named it XMLFile.xml & write the below XML text to test:

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

If still can not create the file check below steps:

Now add a DataList server side control in your page and configure an XmlDataSource to point to the XMLFile.xml. (If you dont know how to configure a XMLDataSource then follow my below demonstration video). Almost done. Need a slight modification in XmlDataSource and DataList control. Modify as per below code:
Modify XmlDataSource:

    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile.xml" XPath="//Customers/Customer">

Modify DataList Control to Add ItemTemplate:

    <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1">
            <HeaderTemplate>Customer Name</HeaderTemplate>
        <ItemStyle BackColor="Gray" ForeColor="Yellow" />
        <AlternatingItemStyle BackColor="Silver" />
            <%# XPath("Name")%>

Not yet understand? Check the below screenshots. This process is same for both C# and VB.Net.

Definitely your XML file will be different in practical projects. So do some tweak in XPath to display Address or City column.

Hope now you can run the example. If can’t, download source code(C# or VB.Net) from below.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, DataList, VB.Net, XML

Leave a Reply

Your email address will not be published. Required fields are marked *