Serializing a DataSet
ADO.NET is captured internally as XML. The reason for this has to do with XML being (1) self-describing , (2) an open standard, and (3) captured as text, which is easily transportable over networks like the Internet.
Because ADO.NET is stored as XML it becomes easy to send DataSet s ”ADO.NET objects ”over the Internet, persist them in an external file, or serialize them for purposes like caching. For example, since a DataSet is serializable, it can be stored in the session cache in ASP.NET Web applications. In fact, a DataSet can be cached using either of the in-process caching mechanisms or the two out-of-process caching mechanisms. (The out-of-process session caching mechanisms are aspnet_state.exe and SQL Server.)
Serialization for session caching and returning DataSet s from XML Web Services happens automatically, but to facilitate your understanding of what is occurring, I have included a short example that demonstrates how to serialize a DataSet to a memory stream. Listing 12.11 serializes the DataSet to a stream and displays the serialized version of the DataSet in a TextBox control.
Listing 12.11 Serializing a DataSet to XML
1: Imports System.Data 2: Imports System.Data.OleDb 3: Imports System.Configuration 4: Imports System.IO 5: Imports System.Text.ASCIIEncoding 6: 7: Public Class Form1 8: Inherits System.Windows.Forms.Form 9: 10: [ Windows Form Designer generated code ] 11: 12: Private Products As DataSet 13: Private Sub Form1_Load(ByVal sender As Object, _ 14: ByVal e As System.EventArgs) Handles MyBase.Load 15: 16: LoadDataSet() 17: DataGrid1.DataSource = Products.Tables(0) 18: SerializeDataSet(Products) 19: 20: End Sub 21: 22: Private Sub LoadDataSet() 23: Dim Connection As OleDbConnection = _ 24: New OleDbConnection(ConfigurationSettings. _ 25: AppSettings("ConnectionString")) 26: 27: Dim Adapter As OleDbDataAdapter = _ 28: New OleDbDataAdapter("SELECT * FROM PRODUCTS", _ 29: Connection) 30: 31: Products = New DataSet("Products") 32: 33: Adapter.Fill(Products) 34: End Sub 35: 36: Private Sub SerializeDataSet(ByVal Data As DataSet) 37: 38: Dim Stream As MemoryStream = New MemoryStream() 39: Products.WriteXml(Stream) 40: Stream.Position = 0 41: TextBox1.Text = System.Text.ASCIIEncoding. _ 42: ASCII.GetString(Stream.GetBuffer()) 43: 44: End Sub 45: 46: End Class
The first half of the listing initializes a DataSet with the data from the Products table. Lines 36 through 44 serialize the DataSet and data as XML into a MemoryStream object, which in turn is used to convert the XML to a string. MemoryStream objects return an array of bytes, so I employed the shared method System.Text.ASCIIEncoding.ASCII.GetString to convert the array of bytes to a string.
You can write the XML form of the DataSet back to a DataSet object by using DataSet.ReadXml . You also have the option of using the more verbose XmlSerializer method to serialize and deserialize serializable objects, but the member methods ReadXml and WriteXml work best for DataSet objects.