AddThis Social Bookmark Button


ADO.NET, Part 3
Pages: 1, 2

The DataSet's XML Capabilities

The DataSet class has several methods for reading and writing data as XML, including:

Returns a string containing an XML representation of the data in the DataSet object.

Returns a string containing the XSD schema for the XML returned by the GetXml method.

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.

Writes the XSD schema for the DataSet to a Stream object, a file, a TextWriter object, or an XmlWriter object.

Reads the XML written by the WriteXml method.

Reads the XSD schema written by the WriteXmlSchema method.

Example 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 SqlConnection(strConnection)
cn.Open(  )
' Set up a data adapter object.
Dim strSql As String = "SELECT * FROM Customers" _
   & " WHERE CustomerID = 'GROSR'"
Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, cn)
' Load a data set.
Dim ds As DataSet = New DataSet("MyDataSetName")
da.Fill(ds, "Customers")
' Set up a new data adapter object.
strSql = "SELECT Orders.*" _
   & " FROM Customers, Orders" _
   & " WHERE (Customers.CustomerID = Orders.CustomerID)" _
   & "    AND (Customers.CustomerID = 'GROSR')"
da = New SqlDataAdapter(strSql, cn)
' Load the data set.
da.Fill(ds, "Orders")
' Close the database connection.
cn.Close(  )
' Create a relation.
ds.Relations.Add("CustomerOrders", _
   ds.Tables("Customers").Columns("CustomerID"), _
' Save as XML.

Related Reading

Programming Visual Basic .NETProgramming Visual Basic .NET
By Dave Grundgeiger
Table of Contents
Sample Chapter
Full Description

The majority of the code in Example 8-5 simply loads the DataSet with data. Actually writing the XML is done with the DataSet's WriteXml method at the end of Example 8-5. The contents of the file thus created are shown in Example 8-6. Some lines in Example 8-6 have been wrapped for printing in this book.

Example 8-6: The file produced by the code in Example 8-5

<?xml version="1.0" standalone="yes"?>
    <ContactName>Manuel Pereira</ContactName>
    <Address>5th Ave. Los Palos Grandes</Address>
    <Phone>(2) 283-2951</Phone>
    <Fax>(2) 283-3397</Fax>
    <ShipAddress>5th Ave. Los Palos Grandes</ShipAddress>
    <ShipAddress>5th Ave. Los Palos Grandes</ShipAddress>

The syntax of this overloaded version of the WriteXml function is:

Public Overloads Sub WriteXml(ByVal 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 class's WriteXml method can be read back into a DataSet object using the ReadXml method. Example 8-7 reads back the file written by the code in Example 8-5.

Example 8-7: Recreating a DataSet object from XML

Dim ds As New DataSet(  )

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.

The next installment will focus on binding DataSets.

O'Reilly & Associates recently released (December 2002) Programming Visual Basic .NET.

View catalog information for Programming Visual Basic .NET

Return to the .NET DevCenter.