Runtime Bind XML File Data into Asp.Net Repeater control using C# VB.Net

Most of the times we need to add different kind of data sources in our Data Controls. Repeater Control also a popular control. XML file also a popular Data Source. That’s why in this article I will explain “How we can Bind XML File into a Repeater control using C# VB.Net

Runtime Bind XML File Data into Asp.Net Repeater control using C# VB.Net

Create an XML File Like Below:

<?xml version="1.0" encoding="utf-8" ?>
  < Id="0001" Title="Bind Dropdown List" Visit="985" Modified="Jan 01, 2009">
  < Id="0002" Title="Event calendar" Visit="777" Modified="Feb 01, 2009">
  < Id="0003" Title="Select all checkboxes" Visit="853" Modified="Mar 01, 2009">
  < Id="0004" Title="Transfer data between" Visit="957" Modified="Apr 01, 2009">

Note: Named it “XMLFile.xml”.

Prepare the Asp.Net Repeater Control Like Below:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Repeater ID="Repeater1" runat="server">
        <table border="1px" cellpadding="5" cellspacing="0" style="text-align:center;border-style:solid;border-collapse:collapse;">
        <tr style="background:CornflowerBlue;Color:White;">
            <th>Modified Data</th>
            <td><%# Eval("ID")%></td>
        <tr style="background:#99CCFF;">
            <td><%# Eval("ID")%></td>

Now Bind XML DataSource into Repeater Control:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            DataSet XMLDataSet;
            string FilePath = Server.MapPath("XMLFile.xml");
            XMLDataSet = new DataSet();

            //Read the contents of the XML file into the DataSet
            Repeater1.DataSource = XMLDataSet.Tables[0].DefaultView;

Note: Don’t forget to add “System.Data” namespace.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim XMLDataSet As DataSet
            Dim FilePath As String

            FilePath = Server.MapPath("XMLFile.xml")
            XMLDataSet = New DataSet()

            'Read the contents of the XML file into the DataSet
            Repeater1.DataSource = XMLDataSet.Tables(0).DefaultView
        End If
    End Sub

Note: Don’t forget to add “System.Data” namespace.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code