6.8. Deserializing Data
Problem
You have a DataSet that has been serialized and written to a file. You want to re-create the DataSet from this file.
Solution
Deserialize the file contents and cast the result to a DataSet object.
The solution loads a file stream containing a previously serialized DataSet in XML, SOAP, and binary formats and deserializes it to recreate the original DataSet. Specifically, the solution uses the files created in the solution for Recipe 6.7. Copy these files to the directory that contains the solution file DeserializeData.sln.
You need to add references to the System.Runtime.Serialization.Formatters.Soap assembly to build the solution.
The C# code in Program.cs in the project DeserializeDate is shown in Example 6-8.
Example 6-8. File: Program.cs for DeserializeData solution
using System;
using System.Data;
using System.Data.SqlClient;
// Namespaces used for deserialization
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
namespace DeserializeData
{
class Program
{
static void Main(string[] args)
{
string fileName = @"..\..\..\SerializedDataSet";
Stream stream;
DataSet ds;
// Deserialize using DataSet.ReadXml( )
using (stream = File.Open(fileName + "_1.xml", FileMode.Open,
FileAccess.Read))
{
ds = new DataSet( ); ds.ReadXml(stream); } Console.WriteLine("{0} deserialized using DataSet.ReadXml( ).", fileName + "_1.xml"); OutputDataSetInfo(ds); // Deserialize ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access