Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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" ?>
<IranHistoricalPlaces>
 <Place name="Taghe Bostan">
  <City>Kermanshah</City>
  <Antiquity>2000</Antiquity>
 </Place>
 <Place name="Persepolis">
  <City>Shiraz</City>
  <Antiquity>2500</Antiquity>
 </Place>
</IranHistoricalPlaces>

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

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

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="http://nayyeri.net/rss.aspx" 
XPath="rss/channel/item" runat="server"></asp:XmlDataSource>
<asp:DataList ID="DataList" runat="server" DataSourceID="XmlDataSource">
 <ItemTemplate>
     <font face="tahoma" size="6"><Strong><%#XPath("title")%><br />
     </Strong></font>
     <%#XPath("description")%><br />
     <font color="navy"><i><%#XPath("pubDate")%><br /></i></font>
 </ItemTemplate>
</asp:DataList>

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:

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="http://nayyeri.net/rss.aspx" 
XPath="rss/channel/item" runat="server"></asp:XmlDataSource>
<asp:DataList ID="DataList" runat="server" DataSourceID="XmlDataSource">
 <ItemTemplate>
     <font face="tahoma" size="6"><Strong><I><%#XPath("title")%></I><br />
     </Strong></font>
     <%#XPath("description")%><br />
     <font color="navy"><i><%#XPath("pubDate")%><br /></i></font>
 </ItemTemplate>
</asp:DataList>

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.

Bugs

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.

History

  • 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.

License

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

Share

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

Comments and Discussions

 
QuestionMy Vote Of 5 PinmemberAlireza_13622-Feb-13 21:16 
GeneralSiteMapDataSource Control in ASP.Net PingroupAnkitaaguggi13-Oct-11 23:08 
GeneralMy vote of 4 PinmemberArt Schwalbenberg18-Aug-11 5:42 
Generaldatelist Pinmembervishalpanchal26-Jul-10 20:47 
How to Edit,Update and Delete in the Datalist
GeneralIs possible to put if logic on &lt;%#XPath("description") %&gt; PinmemberAbhay Mhatre12-Jun-09 11:12 
GeneralRe: Is possible to put if logic on &lt;%#XPath("description") %&gt; Pinmemberrobert_smith7614-Jul-09 7:20 
Questionlimiting the number of records/data items PinmemberErandav25-Jan-09 21:25 
QuestionHow to get XML from asp:XmlDataSource Pinmemberfthavha2-Dec-08 19:26 
AnswerRe: How to get XML from asp:XmlDataSource Pinmemberfthavha4-Dec-08 1:55 
QuestionCan we bind the datafile to the xmldatasource dynamically Pinmembersatigv9-May-08 4:46 
Questionlittle problem PinmemberANANASANANAS13-Aug-07 23:51 
Questioninsert, delete and update? Pinmemberitinko28-Mar-07 5:14 
AnswerRe: insert, delete and update? Pinmemberyogiberr30-May-07 12:14 
GeneralLocal XML Pinmemberggomez03922-Feb-07 5:22 
GeneralSupport Complex Structure Pinmembereng_Hosam Kamel19-Jan-06 6:27 
GeneralRe: Support Complex Structure PinmemberKeyvan Nayyeri19-Jan-06 16:25 
GeneralRe: Support Complex Structure PinmemberKeyvan Nayyeri23-Jan-06 7:32 
QuestionPaging solution? PinmemberChris Keeble16-Nov-05 21:44 
AnswerRe: Paging solution? PinmemberMike Chaliy1-May-06 13:37 
GeneralASP.NET 2.0 XmlDataSource's XPath doesn't support namespaces PinmemberDonXML5-Jul-05 9:31 
GeneralRe: ASP.NET 2.0 XmlDataSource's XPath doesn't support namespaces PinmemberKeyvan Nayyeri5-Jul-05 14:40 
GeneralRe: ASP.NET 2.0 XmlDataSource's XPath doesn't support namespaces PinmemberDonXML5-Jul-05 15:27 
GeneralRe: ASP.NET 2.0 XmlDataSource's XPath doesn't support namespaces PinmemberKeyvan Nayyeri5-Jul-05 16:33 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 4 Jul 2005
Article Copyright 2005 by Keyvan Nayyeri
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid