XMLDataSource with an example

This example describes how to implement XMLDataSource in your asp.net application. XMLDatasource is used to have the XML as a DataSource and bind it to the controls.

Introduction :

XMLDataSource is a control, which has the ability to get the XML from the XMLfile and wrap it inside the control.
Let us see how we can sue XMLDataSource in our application.

Building XML File :

First thing we need to do is to build an xml file to use it as a XMLDataSource

<?xml version="1.0" encoding="utf-8" ?>
<Items name="House"></Items>
<Items name="Credit Card"></Items>
<Items name="Car"></Items>
<Items name="Bike"></Items>
<Items name="Laptop"></Items>
<Items name="Desktop"></Items>
<Items name="Loans"></Items>
<Items name="Debit Card"></Items>
<Items name="CD Player"></Items>
<Items name="Mobile"></Items>

Please name this xml file as "Sample.xml"

Adding XMLDataSource control in aspx Page:

In the aspx page, add the XmlDataSource control and set the DataFile to the path where you have stored the Sample.xml file
In this example, i have stored the xml file in the path "/App_Data/Sample.xml"
After setting the path for the datafile,
i'm binding the values in the Sample.xml file into the CheckboxList control by setting the DataSourceID to the ID of the XMLDataSource

I have designed the aspx page using the below code samples

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>XMLDataSource - Sample</title>
<form id="form1" runat="server">
<asp:XmlDataSource ID="MyXmlDataSource" runat="server" DataFile="/App_Data/Sample.xml">
<b>Please select the things you Own !</b>
<br /><br /><br />
<asp:CheckBoxList ID="ThingsOwn" runat="server" DataSourceID="MyXmlDataSource" DataMember="Items" DataTextField="Name"


Now it's almost done, I have just set the DataMember and DataTextField for the CheckBoxList.
Execute the page to see the XML Content populated as the Text in the CheckBoxListControl.


Author: Lukman kc08 Dec 2011 Member Level: Bronze   Points : 0

Its a helpful script

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: