INFO: Serialization of Public and Private Fields of a DataSet (326504)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)

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

  1. Create a new Visual Basic .NET project of Console Application type. By default, the Module1.vb file is displayed.
  2. 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("Object is Not Serializable")
          End If
    
          Console.WriteLine("Following members of DataSet are Serializable")
    
          ' 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
  3. 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

  1. Create a new Visual Basic .NET project of type Console Application. By default, the Module1.vb file is displayed.
  2. 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
    
  3. 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 = "My DataSet"
          dsEmployee.Namespace = "microsoft.com"
          dsEmployee.Prefix = "DataSetSample"
    
          ' Add a Table to DataSet
          dsEmployee.Tables.Add("Employee")
          ' Add Columns to Table
          colTable = New DataColumn("Emp_ID", GetType(Integer))
          dsEmployee.Tables(0).Columns.Add(colTable)
    
          colTable = New DataColumn("Skill_ID", GetType(Integer))
          colTable.DefaultValue = 1
          dsEmployee.Tables(0).Columns.Add(colTable)
    
          colTable = New DataColumn("Emp_DOJ", GetType(DateTime))
          colTable.DefaultValue = "01/01/2002 09:30:00 AM"
          dsEmployee.Tables(0).Columns.Add(colTable)
    
          ' Add a Row to Table
          rowTable = dsEmployee.Tables(0).NewRow()
          rowTable("Emp_ID") = 1
          rowTable("Skill_Id") = 5
          dsEmployee.Tables(0).Rows.Add(rowTable)
    
          ' Add another Table to Create Relationship
          dsEmployee.Tables.Add("Skills")
          colTable = New DataColumn("Skill_ID", GetType(Integer))
          colTable.DefaultValue = 1
          dsEmployee.Tables(1).Columns.Add(colTable)
    
          colTable = New DataColumn("Skill_Desc", GetType(String))
          dsEmployee.Tables(1).Columns.Add(colTable)
    
          rowTable = dsEmployee.Tables(1).NewRow()
          rowTable("Skill_Id") = 5
          rowTable("Skill_Desc") = "Visual Basic .NET"
          dsEmployee.Tables(1).Rows.Add(rowTable)
    
  4. 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("Skill_Id")
          colChild = dsEmployee.Tables(1).Columns("Skill_Id")
          drRelation = New DataRelation("FK_skill_id", colParent, colChild, True)
          dsEmployee.Relations.Add(drRelation)
  5. 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("DataSet Properties Before Serialization")
          Console.WriteLine("CaseSensitive: {0}", dsEmployee.CaseSensitive)
          Console.WriteLine("DataSetName: {0}", dsEmployee.DataSetName)
          Console.WriteLine("Namespace: {0}", dsEmployee.Namespace)
          Console.WriteLine("Prefix: {0}", dsEmployee.Prefix)
          Console.WriteLine("DataRelation Name: {0}", dsEmployee.Relations(0).RelationName)
          Console.WriteLine("Default Value for Employee Date of Joining: {0}", dsEmployee.Tables(0).Rows(0)("Emp_DOJ"))
    
          ' Serialize the DataSet
          Dim writeDs As Stream = New FileStream("c:\EmployeeDataSet.bin", FileMode.OpenOrCreate)
          fmtServices.Serialize(writeDs, dsEmployee)
          writeDs.Close()
    
  6. 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("c:\EmployeeDataSet.bin", FileMode.Open)
          dsAfterSerialization = fmtServices.Deserialize(readDs)
          readDs.Close()
    
          ' Display the Deserialization results
          Console.WriteLine("-------------------------------------------------------")
          Console.WriteLine("DataSet Properties After Serialization:")
          Console.WriteLine("CaseSensitive: {0}", dsAfterSerialization.CaseSensitive)
          Console.WriteLine("DataSetName: {0}", dsAfterSerialization.DataSetName)
          Console.WriteLine("Namespace: {0}", dsAfterSerialization.Namespace)
          Console.WriteLine("Prefix: {0}", dsAfterSerialization.Prefix)
          Console.WriteLine("DataRelation Name: {0}", dsAfterSerialization.Relations(0).RelationName)
          Console.WriteLine("Default Value for Employee Date of Joining: {0}", dsAfterSerialization.Tables(0).Rows(0)("Emp_DOJ"))
  7. 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.

REFERENCES

For additional information about serialization in the .NET Framework, click the following article numbers to view the articles in the Microsoft Knowledge Base:

314150 INFO: Roadmap for XML Serialization in the .NET Framework

325691 PRB: XML Serialization: System.Xml.XmlSerializer Does Not Serialize Default Values

318039 HOW TO: Make a Typed DataSet Return a Default Value Instead of DBNull by Using Visual Basic .NET

Notice


Modification Type:MajorLast Reviewed:9/4/2003
Keywords:kbSqlClient kbRegistry kbConsole kbDataObject kbinfo KB326504 kbAudITPRO kbAudDeveloper