Creating a DataSet from an XML File

for RuBoard

Creating a DataSet from an XML File

Internally, a DataSet is represented by XML. This enables a DataSet to accurately mirror data returned by any data source. In fact, this is one of the reasons the DataSet object in ADO.NET is so much more powerful than the recordset object in ADO 2.7. Not only does the DataSet store data, but it also stores data about the schema that was used to store the data. Because DataSet s are essentially XML entities, moving data from a DataSet to XML and from XML into a DataSet is a painless and straightforward process.

The DataSet object has an overloaded method named ReadXml() that enables you to read XML data from a string, stream, or file. The example in Listing 10.2 demonstrates how to transfer the data and the data's schema from a file located in the root of the C drive.

Listing 10.2 Creating a DataSet from an XML Document
 <%@ Import Namespace="System.Data" %> <HTML> <HEAD>    <LINK rel="stylesheet" type="text/css" href="Main.css">    <script language="VB" runat="server" >       Sub Page_Load(Source as Object, E as EventArgs)          Dim dsCustomers as DataSet = new DataSet()          dsCustomers.ReadXml("C:\Customers.xml", XmlReadMode.InferSchema)          employees.DataSource = dsCustomers.Tables(0)          employees.DataBind()       End Sub    </script> </HEAD> <BODY> <h1>Creating a DataSet from an XML File</h1> <hr> <form runat="server" id=form1 name=form1>    <asp:DataGrid id="employees" runat="server"></asp:DataGrid> </form> <hr> </BODY> </HTML> 

In line 10 of Listing 10.2, a new DataSet is created. Then, in the next line, the ReadXml() method of the DataSet is used to load the data and schema present in an XML file on the hard disk. Lines 13 “14 bind the DataSet to a DataGrid for display on the page. Incidentally, the Customers.xml file used to load the DataSet in Listing 10.2 was created using the WriteXml() method of the DataSet , covered in detail later in this hour in the section "Writing a DataSet to an XML File."

After the example in Listing 10.2 is run, you should see a screen much like the one in Figure 10.1. The customer data located in the XML document is loaded into the DataSet and then bound to a DataGrid Web control in order to display on a Web form.

Figure 10.1. A DataGrid Web control bound to XML data retrieved from a file.

graphics/10fig01.jpg

As mentioned, the ReadXml() method of the DataSet object performs the magic of locating and loading the XML file. It's a very robust method, with a few different ways to use it. Depending on what objects you pass ReadXml() , it will perform different actions:

  • ReadXml( XmlReader reader , XmlReadMode mode ) ” By passing an XmlReader as the first argument, ReadXml() will read data from an XmlReader object. To learn more about the XmlReader object, see the last section of this hour.

  • ReadXml( Stream stream , XmlReadMode Mode ) ” By passing a stream as the first argument, ReadXml() will read data from a stream.

  • ReadXml( String fileName , XmlReadMode Mode ) ” As you saw in Listing 10.2, by passing a filename to ReadXml() , it will retrieve XML from a file.

Notice that the second argument of the ReadXml() method is of type XmlReadMode . This class exists in the System.Data namespace. The different values and explanations of those values can be found in Table 10.1. If the XmlReadMode argument is omitted from the ReadXml() method call, it automatically defaults to "Auto," as seen in the first entry in Table 10.1.

Table 10.1. XmlReadMode Options for Retrieving XML
Code Symbol
Auto ReadXml() automatically chooses XmlReadMode by examining the XML document.
ReadSchema ReadXml() reads XML schema and loads both data and schema into DataSet .
IgnoreSchema ReadXml() completely ignores XML schema and attempts to load data into DataSet using existing DataSet schema.
InferSchema ReadXml() ignores explicit schema information in the XML using the structure of the XML data as the schema.
DiffGram ReadXml() will read the XML as a DiffGram , appending and merging rows as needed.
Fragment ReadXml() will read the document as partial XML and import the data matching the DataSet schema and ignore the rest.

Serialization

Serialization is defined as "a way of saving the state of an object." In Microsoft .NET, serialization usually refers to the conversion of an object's values into XML. Deserialization normally refers to the reverse process of building an object based on saved values in an XML document.

for RuBoard


Sams Teach Yourself ADO. NET in 24 Hours
Sams Teach Yourself ADO.NET in 24 Hours
ISBN: 0672323834
EAN: 2147483647
Year: 2002
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net