Displaying XML Data in a DataGrid Demo

This demo illustrates how to display XML data through a DataGrid Web control. We load the XML data into a DataSet by using the DataSet's ReadXml method. Then, the DataSet is bound to the DataGrid's DataSource property. Finally, the DataGrid's DataBind() method is called.


The Contents of the XML File books.xml
titleauthoryear
Teach Yourself Active Server Pages 3.0 in 21 DaysMitchell1999
Designing Active Server PagesMitchell2000
ASP.NET: Tips, Tutorials, and CodeMitchell2001
ASP UnleashedWalther1998
ASP.NET UnleashedWalther2002
Creating Data Driven ASP.NET ApplicationsSeven2002

A Nicer Looking DataGrid Representation of the XML File books.xml

TitleAuthorYear
Teach Yourself Active Server Pages 3.0 in 21 DaysMitchell1999
Designing Active Server PagesMitchell2000
ASP.NET: Tips, Tutorials, and CodeMitchell2001
ASP UnleashedWalther1998
ASP.NET UnleashedWalther2002
Creating Data Driven ASP.NET ApplicationsSeven2002


Source Code
<%@ import Namespace="System.Data" %>
<script runat="server">
    sub Page_Load(sender as Object, e as EventArgs)
        Dim myDataSet as New DataSet()
    
        myDataSet.ReadXml(Server.MapPath("books.xml"))
    
        dgBooks.DataSource = myDataSet
        dgBooks.DataBind()

        dgBooksPretty.DataSource = myDataSet
        dgBooksPretty.DataBind()
    end sub
</script>

<html>
<body>
  <b>The Contents of the XML File 
  <a href="/demos/books.xml"><code>books.xml</code></a></b><br />
  <asp:datagrid id="dgBooks" runat="server" />
  
  <p align="center">  
  <b>A Nicer Looking DataGrid Representation of the XML File 
  <a href="/demos/books.xml"><code>books.xml</code></a></b><br />
  </p>
  <asp:datagrid id="dgBooksPretty" runat="server"
                AutoGenerateColumns="False"
                Font-Name="Verdana"
                Font-Size="Small"
                HorizontalAlign="Center"
                ItemStyle-BackColor="#FFFFCC"
                AlternatingItemStyle-BackColor="#EEEEEE">
                
      <HeaderStyle BackColor="Red" HorizontalAlign="Center" 
                   ForeColor="White" Font-Bold="True" />
               
      <Columns>
        <asp:BoundColumn HeaderText="Title" DataField="title" />
        <asp:BoundColumn HeaderText="Author" DataField="author" />
        <asp:BoundColumn HeaderText="Year" DataField="year" />     
      </Columns>
  </asp:datagrid>
  


Return to the article