Tales from the Evil Empire

Bertrand Le Roy's blog


Bertrand Le Roy

BoudinFatal's Gamercard

Tales from the Evil Empire - Blogged

Blogs I read

My other stuff


Using the XmlDataSource Data property for easy samples

Here's a little trick to write samples that involves data binding controls to a database. It's very inconvenient to have to package a sample database with your code (especially when it's just a few lines of code in a blog), and just making sure your users have SQL Server installed is no small thing. All this just diverts the attention from what really matters: your sample code.

It's a little-known feature that XmlDataSource enables you to declaratively expose sample data to the page. Here's how it's done:

<asp:XmlDataSource runat="server" ID="data">
            <color name="Red" value="#FF0000"/>
            <color name="Green" value="#00FF00"/>
            <color name="Blue" value="#0000FF"/>
            <color name="Cyan" value="#00FFFF"/>
            <color name="Purple" value="#FF00FF"/>
            <color name="Yellow" value="#FFFF00"/>
<asp:DataList runat="server" ID="Repeat" DataSourceID="data" DataMember="color">
        <asp:Label runat="server" ID="ColorLabel" Text='<%# Eval("name") %>' />

The Data property of the datasource can contain an arbitrary XML document. You can define pseudo-tables in there (like the color pseudo-table in this example) just by adding elements with the same name under the root of the document. The columns are defined by the attributes.

As far as the data controls that consume the datasource are concerned, the XmlDataSource behaves exactly like a database and all you need to run the sample is the code.

Hope this helps.

Posted: Jul 28 2006, 11:34 PM by Bertrand Le Roy | with 8 comment(s) |
Filed under: ,


Kris van der Mast said:

Hi, great example. But in order to get it to work I had to import the namespace System.ComponentModel and create a Button_Click eventhandler. The Button_Click event can be easily added by selecting Edit Template on the DataList control and double clicking the button. The namespace can be imported by adding this statement to the top of the page: Grz, Kris.
# July 29, 2006 9:42 AM

Bertrand Le Roy said:

Kris: you're right. I've simplified the sample and removed these requirements so that one can concentrate on the datasource, which is the really important part. Thanks for the heads up.

# July 29, 2006 3:06 PM

mike said:

Note that XmlDataSource defaults to assuming that "column values" are represented as attributes rather than child elements; in the latter case, you have to add a transform for it to work. Also, inline XML can't be updated, right? So if your sample wants to illustrate update, things are a bit more complex ...
# July 29, 2006 3:56 PM

Kevin Nixon said:

how do i set an id in xml that will increment by one every time i add a new table? ex red blue
# July 29, 2006 9:22 PM

Bertrand Le Roy said:

Kevin: not sure I understand the question. This is literal contents, so you just set any id attribute you want to each of the records.

# July 30, 2006 4:07 AM

vikram said:

This is a good stuff. THis would help me when providing with example to some other people
# August 25, 2006 1:06 AM