When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Book Reviews
Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
Web Hosts
Author an Article
spgif spgif

ASP ASP.NET ASP FAQs Feedback topnav-right
Print this Page!
Published: Wednesday, May 29, 2002

XML, the DataSet, and a DataGrid

By Scott Mitchell


With classic ASP, if one wanted to display XML data on an ASP Web page they had a number of options. The simplest option, in my opinion, would be to use the XML support present in ADO 2.5 to populate a Recordset with XML data, and then to display the Recordset data as you would on any other page. Another option would be to use the MSXML component to iterate through the contents of the XML document, perhaps using XSLT. Similarly, if you wanted to write a collection of data, such as database results, to an XML stream would be, again, to use the Recordset object or to use the MSXML component. (For a more information on working with XML with the MSXML component be sure to read: XML and XSL with ASP; for more information on saving a Recordset's contents as XML be sure to read: Creating a Function to Stream a Recordset to XML.)

- continued -

With ASP.NET you have a number of options. If you are simply wanting to display an XML document formatted via an XSL source you can use the XML server control (<asp:xml runat="server" ... />). If you are needing to iterate through the document piecemeal and perform various operations on each node, you may want to use the classes in the System.Xml namespace, which provide similar functionality as the MSXML component. (For more information on using the XML server control be sure to read HOW TO: Display an XML Document in ASP.NET by Using the Xml Web Server Control.)

While the previous two methods will work just fine, I've found it quite simpler to use a tool I'm much more familiar with: the DataGrid. (Be sure to read An Extensive Examination of the DataGrid for more information on this very useful and powerful Web control!) The DataGrid provides a much easier API to control the final output than an XSL document does (in my opinion) and, as we've seen in previous articles on the DataGrid, there is a wide array of useful tasks you can perform with the DataGrid, from sorting to associating custom actions with row-bound buttons.

This article examines how to easily display XML data through a DataGrid in an ASP.NET Web page. It also looks at how to easily write the contents of a database query to XML!

Reading an XML Source into a DataSet

In a previous article, Efficiently Iterating through the Results of a Database Query using ADO.NET, we examined how to use the DataReader Web control to retrieve a set of Database results. In the many parts of the Extensive Examination of the DataGrid articles, we examines how to bind a DataReader to a DataGrid. You can also bind other types of objects to the DataGrid's DataSource property to achieve the same effect. One such object is the DataSet.

Recall that the DataReader comes in various flavors - there's the SqlDataReader, for reading data from the SQL data provider, and an OleDbDataProvider, for reading data from the OleDb data provider. Furthermore, in order to read a DataReader a connection must be established to the database. That is, if you populate a DataReader and then disconnect from the Database, you can no longer read from the DataReader.

The DataSet is just about the exact opposite from the DataReader. The DataSet is provider-neutral, meaning that the same object is used regardless if you are reading the data from a SQL provider, an OleDb provider, or from an XML file. Additionally, the DataSet is a disconnected data storage object, meaning that you can populate the DataSet and then disconnect the data provider and still read from the DataSet. DataSet's are much richer objects than DataReaders; they contain detailed information about the data held within them. Not surprisingly a DataSet provides more power, but is less efficient than a DataReader.

DataSets can very easily be populated from the contents of a valid XML file. Simply call the ReadXml method. The below example shows how to read an XML file from disk and bind it to a DataGrid:

<%@ Import Namespace="System.Data" %>
<script runat="server">
    sub Page_Load(sender as Object, e as EventArgs)
        Dim myDataSet as New DataSet()
        dgBooks.DataSource = myDataSet
    end sub

<asp:datagrid id="dgBooks" runat="server" />
[View a live demo!]

The above code is fairly straightforward. First, the Import directive is used to specify that the System.Data namespace should be imported (this is the namespace that contains the DataSet class). Next, in the Page_Load event handler a new DataSet is created and the ReadXml method is used to read the contents of the XML file books.xml. Finally, the DataGrid dgBooks's DataSource property is set equal to the DataSet and the DataGrid's DataBind() method is called.

Writing a DataSet's Contents to an XML File

You can easily populate a DataSet with data from a SQL database or OleDb-Compliant database. I won't delve into the details on how to do this here since there already exists a plethora of articles on various sites on the topic (see here and here, for example). Given a populated DataSet it is very easy to save the contents to an XML file: simply use the WriteXml method.

The WriteXml method can be used as follows:

<%@ Import Namespace="System.Data" %>
<script runat="server">
    sub Page_Load(sender as Object, e as EventArgs)
        Dim myDataSet as New DataSet()
        'Populate the DataSet somehow
        'Write the DataSet's contents to an XML file
     end sub

This will write the XML data to the specified filename. (If you want to write the data and the XML data schema to the XML file use: myDataSet.WriteXml(filename, XmlWriteMode.WriteSchema).) Pretty straightforward, eh? While the ADO 2.5 Recordset allowed for this functionality, the XML produced was anything but human-readable. Fortunately the ADO.NET DataSet writes out very readable XML, as can be seen by this live demo.

Happy Programming!

  • By Scott Mitchell

    ASP.NET Data Web Controls Kick Start

    ASP.NET Data Web Controls Kick Start

    ASP.NET Data Web Controls Kick Start is author Scott Mitchell's most recent book, which thoroughly examines three of the most commonly used ASP.NET Web controls: the DataGrid, DataList, and Repeater. These three Web controls can be difficult to master due to their numerous features and capabilities. With this book, you'll quickly become an expert, learning the gritty details and true capabilities of each. This 400+ page book explores the topics in this article series in much greater depth, along with examining various topics and techniques not covered here.

    Scott Mitchell is the editor and founder of 4GuysFromRolla.com, author of the An Extensive Examination of the DataGrid Web Control article series, and author of numerous other ASP and ASP.NET books.

    [Buy this Book]
    [Visit the Book's Companion Web Site]

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article