XML, the DataSet, and a DataGrid
By Scott Mitchell
Introduction
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.)
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:
|
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:
|
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!
|