The DataSet’s 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.

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 ...

Get Programming Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.