Serializing DataSets to XML

for RuBoard

Serializing DataSet s to XML

As mentioned in the preceding section, DataSet s are represented internally by XML. Therefore, it is particularly easy to transfer a DataSet to an XML document and also to reverse the process.

Viewing the Contents of a DataSet

The example in Listing 10.3 uses the GetXml() method of the DataSet object to return the DataSet 's contents in XML form as a string. First, a table of information is retrieved from the Customers table in the Northwind database. Then it is placed into a label Web control in a Web form in order to display it.

Listing 10.3 Viewing the Contents of a DataSet
 <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <HTML> <HEAD> <script language="VB" runat="server" >    Sub Page_Load(Source as Object, E as EventArgs)       Dim conn as SqlConnection = new SqlConnection("Initial " + _                      "Catalog=Northwind;Server=(local);UID=sa;PWD=;")       Dim cmd as SqlCommand = new SqlCommand("SELECT * FROM Customers", conn)       Dim adapt as SqlDataAdapter = new SqlDataAdapter(cmd)       Dim dsCustomers as DataSet = new DataSet()       conn.Open()       adapt.Fill(dsCustomers, "Customers")       conn.Close()       lblOutput.Text = dsCustomers.GetXml()    End Sub </script> </HEAD> <body>     <form runat="server">         XML Output:<br>         <asp:label id="lblOutput" runat="server"/>     </form> </body> </html> 

In the example in Listing 10.3, the XML representation of a DataSet is displayed on a Web form. To do this, lines 8 “17 retrieve a DataSet , using methods you've already seen. Then, line 19 uses the GetXml() method of the DataSet object to create a new string containing the DataSet 's XML. This is then displayed in a label Web control in the Web form.

Writing a DataSet to an XML File

Though you could manually store the string returned from the GetXml() method in Listing 10.3 into a file, the DataSet object provides yet another method for working with XML: WriteXml() . Listing 10.4 fills a DataSet with customer information and then writes it to a file using this method.

Listing 10.4 Serializing a DataSet to an XML File in Visual Basic .NET
 <script language="VB" runat="server" >    Sub Page_Load(Source as Object, E as EventArgs)       Dim conn as SqlConnection = new SqlConnection("Initial " + _                      "Catalog=Northwind;Server=(local);UID=sa;PWD=;")       Dim cmd as SqlCommand = new SqlCommand("SELECT * FROM Customers", conn)       Dim adapt as SqlDataAdapter = new SqlDataAdapter(cmd)       Dim dsCustomers as DataSet = new DataSet()       conn.Open()       adapt.Fill(dsCustomers, "Customers")       conn.Close()       dsCustomers.WriteXml("c:\Customers.xml", XmlWriteMode.IgnoreSchema)    End Sub </script> 

Much like the ReadXml() method of the DataSet object, the WriteXml() method uses the XmlWriteMode object to define exactly how the data is handled as it is placed in the file. Table 10.2 gives descriptions of the three modes.

Table 10.2. Three Different XmlWriteMode s
Code Symbol
IgnoreSchema Writes the DataSet as XML without any additional schema information.
WriteSchema Writes the DataSet as XML with additional schema information included as inline XSD schema.
DiffGram Writes the DataSet as a DiffGram .

DiffGram

A DiffGram is an XML document that stores original and current values for the data in a DataSet . This is fairly important because, as you'll see in later hours, the DataSet can automatically apply changes back to the data source by keeping track of which values changed. By serializing a DataSet using this mode, you can ensure that those changes aren't lost.

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