Click here to Skip to main content
13,662,031 members
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 4 Jul 2005

Introduction to XMLDataSource control in ASP.NET 2.0

, 4 Jul 2005
Rate this:
Please Sign up or sign in to vote.
Explains common properties and features of XMLDataSource control in VS.NET 2005.


XML is an important format for storing and retrieving data on the web and the reason is it can be moved between firewalls. We see many websites with RSS and XML output. Many visitors use them to save their time and increase their speed for viewing their favorite websites and web logs. As a developer you should know that Visual Studio .NET 2005 helps you to work with XML data with its new XMLDataSource control.

Using this new control you can insert, delete and update XML data easily in minimum amount of time. In this article I'm going to show you how to use this new control and describe its common properties and features. I'll try to show these features in ASP.NET because this new control is more applicable on the web.

Local XML file

First we need to create this XML file before using our new control: (XMLFile.xml)

<?xml version="1.0" encoding="utf-8" ?>
 <Place name="Taghe Bostan">
 <Place name="Persepolis">

Now I use the following code to show my XML file data in a Repeater control:

<asp:Repeater ID="Repeater" runat="server" DataSourceID="XmlDataSource">
    <Strong><%# XPath("@name") %><br /></Strong>
    <%#XPath("City")%><br />
    <%#XPath("Antiquity")%><br />
<asp:XmlDataSource ID="XmlDataSource" runat="server" DataFile="~/XMLFile.xml" 


In the above code, first I add the XMLDataSource control from the toolbox to my form then I set its DataFile property to the XML file's relative address (~/XMLFile.xml) and its XPath property to "IranHistoricalPlaces/Place". The last one points to our desired level of XML file that we want to work with its nodes.

Now I try to set up my Repeater control for using this XMLDataSource. First set the DataSourceID property to "XMLDataSource" then try to configure this control to show your desired values from XML nodes. I don't want to focus on the formatting codes at this moment. I have used XPath("Nodename") to load my desired data between ItemTemplate tags. If it's an attribute that I want to load into my page I must use @ with its name for my XPath parameter and if it is an element I send its name directly to XPath.

The final result is shown in the following figure:

XML file on the web

We aren't limited to using local XML files. We can use XML files on the web to show in our Web Forms (and it's the major application of this new data control).

Assume that you want to use your favorite RSS in your Web Form. I'll show you how you can do this.

Insert another XMLDataSource control with a DataList control in your Web Form (I prefer using another Web Form). Set the Datafile property to your XML file URL (I used my RSS URL here) then set the DataSourceID property of the DataList control to "XMLDataSource" as shown below:

<asp:XmlDataSource ID="XmlDataSource" DataFile="" 

XPath="rss/channel/item" runat="server"></asp:XmlDataSource>
<asp:DataList ID="DataList" runat="server" DataSourceID="XmlDataSource">
     <font face="tahoma" size="6"><Strong><%#XPath("title")%><br />
     <%#XPath("description")%><br />
     <font color="navy"><i><%#XPath("pubDate")%><br /></i></font>

I wanted to load my post title, description and publish the date in this Web Form. So I set the XPath property of my XMLDataSource to "rss/channel/item" (Look at the structure of my RSS) and used XPath between the ItemTemplate tags of the DataList control.

Here you can see the final result:

<IMG height=215 src="/KB/webforms/XMLDataSource/XMLDataSource2.gif" width=400>

The last point about ItemTemplate tags is that you can change the formatting of your data easily as I have done with HTML tags. For example, here I change my title formatting to Bold/Italic:

<asp:XmlDataSource ID="XmlDataSource" DataFile="" 

XPath="rss/channel/item" runat="server"></asp:XmlDataSource>
<asp:DataList ID="DataList" runat="server" DataSourceID="XmlDataSource">
     <font face="tahoma" size="6"><Strong><I><%#XPath("title")%></I><br />
     <%#XPath("description")%><br />
     <font color="navy"><i><%#XPath("pubDate")%><br /></i></font>

Additional information

These are common applications of XMLDataSource. You can also use this control to save your data into XML files. To do this you need to use the Save method of XMLDataSource control. It will save all the memory cached data into your XML file (as you set in the DataFile property).

The XMLDataSource control doesn't support sorting and paging but you can do them with In-Memory data structures. Surely this won't be useful because you can use other data controls for this purpose.

Smart tag

Smart tags are very useful in ASP.NET 2. So, I'll try to describe the XMLDataSource control Smart Tag here. First look at this Smart tag snapshot:

Using the first choice (Configure Data Source) you can configure important properties of your XMLDataSource. If you open it you can choose Data File, Transform file (that's the XSL file and describes the structure of your Data file) and XPath expression.

The second choice is simple. It's just for refreshing control schema.


There is an important bug in this control and you can read the full story here. Probably Microsoft will fix it in the RTM version.


  • 2005/07/05

    I wrote this article for ASP.NET 2.0 Beta 2. I'll update it for RTM version.

  • 2005/07/07

    On the basis of a comment, I have added the "Bug" topic. I have also added the "Smart Tag" topic in this article.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Keyvan Nayyeri
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
For more information about me just visit my blog @ Nayyeri.NET

You may also be interested in...


Comments and Discussions

QuestionMy Vote Of 5 Pin
Alireza_13622-Feb-13 21:16
memberAlireza_13622-Feb-13 21:16 
GeneralSiteMapDataSource Control in ASP.Net Pin
Ankitaaguggi13-Oct-11 23:08
groupAnkitaaguggi13-Oct-11 23:08 
GeneralMy vote of 4 Pin
Art Schwalbenberg18-Aug-11 5:42
memberArt Schwalbenberg18-Aug-11 5:42 
Generaldatelist Pin
vishalpanchal26-Jul-10 20:47
membervishalpanchal26-Jul-10 20:47 
GeneralIs possible to put if logic on &lt;%#XPath("description") %&gt; Pin
Abhay Mhatre12-Jun-09 11:12
memberAbhay Mhatre12-Jun-09 11:12 
GeneralRe: Is possible to put if logic on &lt;%#XPath("description") %&gt; Pin
robert_smith7614-Jul-09 7:20
memberrobert_smith7614-Jul-09 7:20 
Questionlimiting the number of records/data items Pin
Erandav25-Jan-09 21:25
memberErandav25-Jan-09 21:25 
QuestionHow to get XML from asp:XmlDataSource Pin
fthavha2-Dec-08 19:26
memberfthavha2-Dec-08 19:26 
AnswerRe: How to get XML from asp:XmlDataSource Pin
fthavha4-Dec-08 1:55
memberfthavha4-Dec-08 1:55 
QuestionCan we bind the datafile to the xmldatasource dynamically Pin
satigv9-May-08 4:46
membersatigv9-May-08 4:46 
Questionlittle problem Pin
ANANASANANAS13-Aug-07 23:51
memberANANASANANAS13-Aug-07 23:51 
Questioninsert, delete and update? Pin
itinko28-Mar-07 5:14
memberitinko28-Mar-07 5:14 
AnswerRe: insert, delete and update? Pin
yogiberr30-May-07 12:14
memberyogiberr30-May-07 12:14 
GeneralLocal XML Pin
ggomez03922-Feb-07 5:22
memberggomez03922-Feb-07 5:22 
GeneralSupport Complex Structure Pin
eng_Hosam Kamel19-Jan-06 6:27
membereng_Hosam Kamel19-Jan-06 6:27 
GeneralRe: Support Complex Structure Pin
Keyvan Nayyeri19-Jan-06 16:25
memberKeyvan Nayyeri19-Jan-06 16:25 
GeneralRe: Support Complex Structure Pin
Keyvan Nayyeri23-Jan-06 7:32
memberKeyvan Nayyeri23-Jan-06 7:32 
QuestionPaging solution? Pin
Chris Keeble16-Nov-05 21:44
memberChris Keeble16-Nov-05 21:44 
AnswerRe: Paging solution? Pin
Mike Chaliy1-May-06 13:37
memberMike Chaliy1-May-06 13:37 
GeneralASP.NET 2.0 XmlDataSource's XPath doesn't support namespaces Pin
DonXML5-Jul-05 9:31
memberDonXML5-Jul-05 9:31 
GeneralRe: ASP.NET 2.0 XmlDataSource's XPath doesn't support namespaces Pin
Keyvan Nayyeri5-Jul-05 14:40
memberKeyvan Nayyeri5-Jul-05 14:40 
GeneralRe: ASP.NET 2.0 XmlDataSource's XPath doesn't support namespaces Pin
DonXML5-Jul-05 15:27
memberDonXML5-Jul-05 15:27 
GeneralRe: ASP.NET 2.0 XmlDataSource's XPath doesn't support namespaces Pin
Keyvan Nayyeri5-Jul-05 16:33
memberKeyvan Nayyeri5-Jul-05 16:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 4 Jul 2005
Article Copyright 2005 by Keyvan Nayyeri
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid