1.4. Displaying Data from an XML File
Problem
You want a quick and convenient way to display data from an XML file.
Solution
Use a DataGrid control and
the
ReadXml method of the DataSet
class.
In the .aspx file, add a
DataGridcontrol for displaying the data.
In the code-behind class for the page, use the .NET language of your choice to:
Read the data from the XML file using the
ReadXmlmethod of theDataSetclass.Bind the
DataSetto theDataGridcontrol.
Figure 1-3 shows the appearance of a typical
DataGrid in a browser. Example 1-7 shows the XML used for the recipe. Examples
Example 1-8 through Example 1-10
show the .aspx and code-behind files for an
application that produces this result.

Figure 1-3. DataGrid with XML data output
Discussion
The Page_Load method
in
the code-behind, shown in Example 1-9 (VB) and Example 1-10 (C#), reads the data from the XML file using
the ReadXml method of the
DataSet class, binds the
DataSet to the DataGrid
control, and then performs the necessary cleanup.
Datasets are designed to support hierarchical data and can contain
multiple tables of data. Because of this support, when data is loaded
into the dataset, it is loaded into a Tables collection. In this
example, there is a single node in the XML called Book. The
DataSet will automatically load the XML data into
a table named Book. When binding the data to the
DataGrid, you must reference the desired table ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access