Bind an XMLDataSource to a GridView control

27 06 2007

On one of my ASP.NET 2.0 projects, I had to use an XML file as the data source for a GridView control. The problem was that I had never used this method before. But after doing some research, I found it to be very similar (and straightforward) to binding to a database – with the exception of the need to use XPath.

So…without further adieu, here are the steps and code. Enjoy!

1. Create an XML file that contains your data, like below (taken from Microsoft’s website to save a lot of typing). Save it as books.xml.

<?xml version="1.0"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>
      An in-depth look at creating applications
      with XML.
    </description>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>
      A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.
    </description>
  </book>
</catalog>

2. Add a GridView control to your .aspx page and rename it to gdvBooks.
3. Drag an XmlDataSource control to your form, rename it xdsBooks, and assign your XML file (books.xml) in the control’s DataFile property.
4. Next, change to Source view and input the code below.

<asp:GridView ID="gdvServerVersions" runat="server"         
    AutoGenerateColumns="False" DataKeyNames="id"         
    DataSourceID="xdsServerVersions" EmptyDataText="No data available.">                
    <Columns>
        <asp:BoundField HeaderText="Book ID" DataField="id" 
            SortExpression="id" />
        <asp:TemplateField HeaderText="Author">
            <ItemTemplate>
                <%# XPath("author") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title">
            <ItemTemplate>
                <%# XPath("title") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Genre">
            <ItemTemplate>
                <%# XPath("genre") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <%# XPath("price") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Publish Date">
            <ItemTemplate>
                <%# XPath("publish_date") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <%# XPath("description")%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 

5. That’s it! Simply navigate to your newly created page and see the results.


				

Actions

Informations

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>