Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Introduction to XMLDataSource control in ASP.NET 2.0

0.00/5 (No votes)
4 Jul 2005 1  
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:

"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="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