Using XmlDatasource control in ASP.NET

Created by
Wednesday, August 3, 2016

In this simple tutorial we are going to go over and example of using the XmlDatasource. Read more about the XmlDatasource class here on Microsoft developer network.

To start lets begin by creating an new web forms project in VS. After your project has been set we are going to create a sample Xml file. Add a new Xml file to your project ans call it something along the lines of data.xml.

Add in the following xml data to your file.

<StandardToolBox>  
  <Control Name="AdRotator" />  
  <Control Name="BulletedList" />  
  <Control Name="Button" />  
  <Control Name="Calendar" />  
  <Control Name="CheckBox" />  
  <Control Name="CheckBoxList" />  
  <Control Name="DropDownList" />  
  <Control Name="FileUpload" />  
  <Control Name="HiddenField" />  
  <Control Name="HyperLink" />  
  <Control Name="Image" />  
  <Control Name="ImageMap" />  
  <Control Name="ListBox" />  
  <Control Name="Literal" />  
  <Control Name="Localize" />  
  <Control Name="MultiView" />  
  <Control Name="View" />  
  <Control Name="Panel" />  
  <Control Name="PlaceHolder" />  
  <Control Name="RadioButton" />  
  <Control Name="RadioButtonList" />  
  <Control Name="Substitution" />  
  <Control Name="Table" />  
  <Control Name="TextBox" />  
  <Control Name="Wizard" />  
  <Control Name="XML" />  
</StandardToolBox>

Now lets create our web form. Stick with something straight forward like Default.aspx.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>XmlDataSource - dotnetdevelopernetwork.com</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/data.xml"></asp:XmlDataSource> 
         
        <asp:Label ID="Label1" runat="server" Font-Size="Medium" ForeColor="Fuchsia"></asp:Label>  
        <br /><br />  
        <asp:Label ID="Label2" runat="server" Text="Select: ToolBox Control" Font-Size="Large" ></asp:Label>  
        
        <asp:RadioButtonList  
             ID="RadioButtonList1"  
             runat="server"  
             DataSourceID="XmlDataSource1"  
             DataMember="Control"  
             DataTextField="Name"  
             RepeatColumns="4"  
             AutoPostBack="true"  
             OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"  >  
        </asp:RadioButtonList>  
    </div>  
    </form>  
</body>  
</html>

Your output should be as visible in the image below

Tags:
xml
c#
0
Rating
1 2 3 4 5

Save

Saved

Report

Reported
Reasons
Cancel
0 comments