|
The DataSet Alternative
The .Net platform's DataSet class provides several ways to read, filter, sort, and manipulate XML data
by Dan Wahlin
DataSets are a powerful new addition to the .Net platform that allow both relational and XML data to be read, edited, and bound to different controls. When used properly, DataSets provide an alternative to using the Document Object Model (DOM) classes or the XmlTextReader class to parse XML. Several examples of using XML and DataSets together will show you how you can simplify common tasks and how DataSets can be an alternative to Web services in applications that exchange data.
The DataSet class is part of a family of classes located in ADO.Net, the data access layer of the .Net platform. If you haven't worked with the DataSet class before, you can see a general overview of some of the other classes that are associated with it in Figure 1. These classes are located in the System.Data namespace.
The DataTable object is one of the most important objects that you'll use when working with the DataSet. DataTable objects are composed of collections of DataRow and DataColumn objects and can have relationships, constraints, extended properties, plus more. Unlike the "classic" ADO Recordset, DataSets can hold multiple DataTable objects, and relationships between these objects can be created programmatically, meaning that the data from a database's Customers and Orders tables can be loaded into a DataSet and held in two different DataTable objects. You can create relationships between these tables through referencing primary/foreign keys in the tables.
You can also use a DataView object to create a customized view of the data within a DataTable. DataViews present a useful way to sort, edit, and navigate a group of DataRows and DataColumns and can be bound to different controls found in the .Net platform. We'll see how to use the DataView and several other DataSet-related objects in this discussion.
Loading XML into DataSets
Now that you've seen some of the objects that you'll encounter when working with DataSets, let's take a look at how to load XML into a DataSet by using the ReadXml() method. For example, to load the CustomersOrders.xml document into a DataSet, you can use this code:
System.Data.DataSet ds =
new System.Data.DataSet();
ds.ReadXml(Server.MapPath(
"CustomersOrders.xml"));
Back to top
|