Microsoft KB Archive/326504

= INFO: Serialization of Public and Private Fields of a DataSet =

Article ID: 326504

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q326504



SUMMARY
This article provides information about the public and private fields of a DataSet that can be serialized.

Serialization can be defined as the process of storing the state of an object instance to an .xml file. During serialization, the public and private fields of the object are converted to a stream of bytes and then written to a data stream. When the object is later deserialized, an exact clone of the original object is created.



MORE INFORMATION
You may use the GetSerializableMembers method of the FormatterServices object to obtain a list of the members in the DataSet objects that are serializable.

The following code samples demonstrate how to use GetSerializableMembers to know the members of DataSet that are serializable. You can also see the serializable member of DataTable in a similar manner.

Obtain a List of Serializable Members
 Create a new Visual Basic .NET project of Console Application type. By default, the Module1.vb file is displayed.  In Module1.vb, paste the following code over the existing code: Imports System Imports System.Runtime.Serialization

Module Module1

Sub Main ' Create a FormatterServices Object Dim fmtService As FormatterServices ' Create a DataSet Dim ds As New DataSet Dim dsType As Type = ds.GetType Dim memberInfo As System.Reflection.MemberInfo

' Check if Object is Serializable If dsType.IsSerializable Then memberInfo = fmtService.GetSerializableMembers(dsType) Else Console.WriteLine(&quot;Object is Not Serializable&quot;) End If

Console.WriteLine(&quot;Following members of DataSet are Serializable&quot;)

' Display the Serizable member in Console Dim memInfo As System.Reflection.MemberInfo For Each memInfo In memberInfo Console.WriteLine(memInfo.Name) Next End Sub

End Module  Run the sample code to see the list of members of DataSet that can be serialized.

Note: To run this sample code, you have to create a key in the registry. In registry HKEY_LOCAL_MACHINE\Software\Microsoft\.NETFramework, create a new DWORD value with the name IgnoreSerializationBit and the value of 1.

IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows Registry

Serialize and Deserialize a DataSet
 Create a new Visual Basic .NET project of type Console Application. By default, the Module1.vb file is displayed.  In Module1.vb, add the following code to the Namespace declaration section: Imports System Imports System.IO Imports System.Data Imports System.Data.SqlClient Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Binary   Add the following code to the Sub Main section to create a DataSet: Dim dsEmployee As DataSet Dim colTable As DataColumn Dim rowTable As DataRow ' Create a DataSet dsEmployee = New DataSet dsEmployee.CaseSensitive = True dsEmployee.DataSetName = &quot;My DataSet&quot; dsEmployee.Namespace = &quot;microsoft.com&quot; dsEmployee.Prefix = &quot;DataSetSample&quot;

' Add a Table to DataSet dsEmployee.Tables.Add(&quot;Employee&quot;) ' Add Columns to Table colTable = New DataColumn(&quot;Emp_ID&quot;, GetType(Integer)) dsEmployee.Tables(0).Columns.Add(colTable)

colTable = New DataColumn(&quot;Skill_ID&quot;, GetType(Integer)) colTable.DefaultValue = 1 dsEmployee.Tables(0).Columns.Add(colTable)

colTable = New DataColumn(&quot;Emp_DOJ&quot;, GetType(DateTime)) colTable.DefaultValue = &quot;01/01/2002 09:30:00 AM&quot; dsEmployee.Tables(0).Columns.Add(colTable)

' Add a Row to Table rowTable = dsEmployee.Tables(0).NewRow rowTable(&quot;Emp_ID&quot;) = 1 rowTable(&quot;Skill_Id&quot;) = 5 dsEmployee.Tables(0).Rows.Add(rowTable)

' Add another Table to Create Relationship dsEmployee.Tables.Add(&quot;Skills&quot;) colTable = New DataColumn(&quot;Skill_ID&quot;, GetType(Integer)) colTable.DefaultValue = 1 dsEmployee.Tables(1).Columns.Add(colTable)

colTable = New DataColumn(&quot;Skill_Desc&quot;, GetType(String)) dsEmployee.Tables(1).Columns.Add(colTable)

rowTable = dsEmployee.Tables(1).NewRow rowTable(&quot;Skill_Id&quot;) = 5 rowTable(&quot;Skill_Desc&quot;) = &quot;Visual Basic .NET&quot; dsEmployee.Tables(1).Rows.Add(rowTable) </li>  Append the following code to create a data relationship (using DataRelation) between tables: ' Create Relationship between Tables Dim drRelation As DataRelation Dim colParent As DataColumn Dim colChild As DataColumn colParent = dsEmployee.Tables(0).Columns(&quot;Skill_Id&quot;) colChild = dsEmployee.Tables(1).Columns(&quot;Skill_Id&quot;) drRelation = New DataRelation(&quot;FK_skill_id&quot;, colParent, colChild, True) dsEmployee.Relations.Add(drRelation) </li>  Append the following code to serialize the DataSet to an .xml file: 'Create a Binary Formatter for Serialization Dim fmtServices As BinaryFormatter fmtServices = New BinaryFormatter

Console.WriteLine(&quot;DataSet Properties Before Serialization&quot;) Console.WriteLine(&quot;CaseSensitive: {0}&quot;, dsEmployee.CaseSensitive) Console.WriteLine(&quot;DataSetName: {0}&quot;, dsEmployee.DataSetName) Console.WriteLine(&quot;Namespace: {0}&quot;, dsEmployee.Namespace) Console.WriteLine(&quot;Prefix: {0}&quot;, dsEmployee.Prefix) Console.WriteLine(&quot;DataRelation Name: {0}&quot;, dsEmployee.Relations(0).RelationName) Console.WriteLine(&quot;Default Value for Employee Date of Joining: {0}&quot;, dsEmployee.Tables(0).Rows(0)(&quot;Emp_DOJ&quot;))

' Serialize the DataSet Dim writeDs As Stream = New FileStream(&quot;c:\EmployeeDataSet.bin&quot;, FileMode.OpenOrCreate) fmtServices.Serialize(writeDs, dsEmployee) writeDs.Close </li>  Append the following code to deserialize the .xml file (that you created in an earlier step) to the DataSet: ' Caeate a new DataSet to get Object after Deserialization Dim dsAfterSerialization As DataSet

' Deserialize the binary file to obtain DataSet Dim readDs As Stream = New FileStream(&quot;c:\EmployeeDataSet.bin&quot;, FileMode.Open) dsAfterSerialization = fmtServices.Deserialize(readDs) readDs.Close

' Display the Deserialization results Console.WriteLine(&quot;---&quot;) Console.WriteLine(&quot;DataSet Properties After Serialization:&quot;) Console.WriteLine(&quot;CaseSensitive: {0}&quot;, dsAfterSerialization.CaseSensitive) Console.WriteLine(&quot;DataSetName: {0}&quot;, dsAfterSerialization.DataSetName) Console.WriteLine(&quot;Namespace: {0}&quot;, dsAfterSerialization.Namespace) Console.WriteLine(&quot;Prefix: {0}&quot;, dsAfterSerialization.Prefix) Console.WriteLine(&quot;DataRelation Name: {0}&quot;, dsAfterSerialization.Relations(0).RelationName) Console.WriteLine(&quot;Default Value for Employee Date of Joining: {0}&quot;, dsAfterSerialization.Tables(0).Rows(0)(&quot;Emp_DOJ&quot;)) </li> Run the application, and then verify the results on the console.

You will notice that after deserialization, the DataSet is constructed back to Original Dataset.</li></ol>

<div class="references_section">

Notice
Keywords: kbinfo kbregistry kbsqlclient kbconsole kbdataobject KB326504

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.