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" ?>
<ThingsYouHave>
<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>
</ThingsYouHave>
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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:XmlDataSource ID="MyXmlDataSource" runat="server" DataFile="/App_Data/Sample.xml">
</asp:XmlDataSource>
<b>Please select the things you Own !</b>
<br /><br /><br />
<asp:CheckBoxList ID="ThingsOwn" runat="server" DataSourceID="MyXmlDataSource" DataMember="Items" DataTextField="Name"
RepeatColumns="3"></asp:CheckBoxList>
</div>
</form>
</body>
</html>Conclusion:
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.
Its a helpful script