The DataSets XML Capabilities

362 The return value is the newly created DataRelation object. Ex am ple 8- 4 ignores the return value.

8.7 The DataSets XML Capabilities

The DataSet class has several methods for reading and writing data as XML, including: GetXml Returns a string containing an XML representation of the data in the DataSet object. GetXmlSchema Returns a string containing the XSD schema for the XML returned by the GetXml method. WriteXml Writes the XML representation of the data in the DataSet object to a Stream object, a file, a TextWriter object, or an XmlWriter object. This XML can either include or omit the corresponding XSD schema. WriteXmlSchema Writes the XSD schema for the DataSet to a Stream object, a file, a TextWriter object, or an XmlWriter object. ReadXml Reads the XML written by the WriteXml method. ReadXmlSchema Reads the XSD schema written by the WriteXmlSchema method. Ex am ple 8- 5 shows how to write a DataSet to a file as XML using the WriteXml method. Example 8-5. Saving a DataSet to a file as XML Open a database connection. Dim strConnection As String = _ Data Source=localhost;Initial Catalog=Northwind; _ Integrated Security=True Dim cn As SqlConnection = New SqlConnectionstrConnection cn.Open Set up a data adapter object. Dim strSql As String = SELECT FROM Customers _ WHERE CustomerID = GROSR Dim da As SqlDataAdapter = New SqlDataAdapterstrSql, cn Load a data set. Dim ds As DataSet = New DataSetMyDataSetName da.Fillds, Customers Set up a new data adapter object. strSql = SELECT Orders. _ FROM Customers, Orders _ WHERE Customers.CustomerID = Orders.CustomerID _ 363 AND Customers.CustomerID = GROSR da = New SqlDataAdapterstrSql, cn Load the data set. da.Fillds, Orders Close the database connection. cn.Close Create a relation. ds.Relations.AddCustomerOrders, _ ds.TablesCustomers.ColumnsCustomerID, _ ds.TablesOrders.ColumnsCustomerID Save as XML. ds.WriteXmlc:\temp.xml The majority of the code in Ex am ple 8- 5 simply loads the DataSet with data. Actually writing the XML is done with the DataSets WriteXml method at the end of Ex am ple 8- 5 . The contents of the file thus created are shown in Ex am ple 8- 6 . Some lines in Ex am ple 8- 6 have been wrapped for printing in this book. Example 8-6. The file produced by the code in Ex a m ple 8 - 5 ?xml version=1.0 standalone=yes? MyDataSetName Customers CustomerIDGROSRCustomerID CompanyNameGROSELLA-RestauranteCompanyName ContactNameManuel PereiraContactName ContactTitleOwnerContactTitle Address5th Ave. Los Palos GrandesAddress CityCaracasCity RegionDFRegion PostalCode1081PostalCode CountryVenezuelaCountry Phone2 283-2951Phone Fax2 283-3397Fax Customers Orders OrderID10268OrderID CustomerIDGROSRCustomerID EmployeeID8EmployeeID OrderDate1996-07-30T00:00:00.0000000-05:00OrderDate RequiredDate1996-08-27T00:00:00.0000000-05:00RequiredDate ShippedDate1996-08-02T00:00:00.0000000-05:00ShippedDate ShipVia3ShipVia Freight66.29Freight ShipNameGROSELLA-RestauranteShipName ShipAddress5th Ave. Los Palos GrandesShipAddress ShipCityCaracasShipCity ShipRegionDFShipRegion ShipPostalCode1081ShipPostalCode ShipCountryVenezuelaShipCountry Orders Orders OrderID10785OrderID CustomerIDGROSRCustomerID EmployeeID1EmployeeID OrderDate1997-12-18T00:00:00.0000000-06:00OrderDate RequiredDate1998-01-15T00:00:00.0000000-06:00RequiredDate 364 ShippedDate1997-12-24T00:00:00.0000000-06:00ShippedDate ShipVia3ShipVia Freight1.51Freight ShipNameGROSELLA-RestauranteShipName ShipAddress5th Ave. Los Palos GrandesShipAddress ShipCityCaracasShipCity ShipRegionDFShipRegion ShipPostalCode1081ShipPostalCode ShipCountryVenezuelaShipCountry Orders MyDataSetName The syntax of this overloaded version of the WriteXml function is: Public Overloads Sub WriteXmlByVal fileName As String The fileName parameter specifies the full path of a file into which to write the XML. The XML document written by the DataSet classs WriteXml method can be read back into a DataSet object using the ReadXml method. Ex am ple 8- 7 reads back the file written by the code in Ex am ple 8- 5 . Example 8-7. Recreating a DataSet object from XML Dim ds As New DataSet ds.ReadXmlc:\temp.xml The XML created by the WriteXml method contains only data—no schema information. The ReadXml method is able to infer the schema from the data. To explicitly write the schema information, use the WriteXmlSchema method. To read the schema back in, use the ReadXmlSchema method. The GetXml and GetXmlSchema methods work the same as the WriteXml and WriteXmlSchema methods, except that each returns its result as a string rather than writing it to a file.

8.8 Binding a DataSet to a Windows Forms DataGrid