serialization

4
Serialization using C# By Salman Mushtaq Page 1 Serialization using C# December 2 2015 When we want to write or read data from files and database, we do not do this directly. Because object is not automatically converted. Casting is not useful here. So, that’s why we use serialization for doing it correctly. Reading & Writing objects using serialization

Upload: salman-mushtaq

Post on 20-Mar-2017

143 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Serialization

Serialization using C#

By Salman Mushtaq Page 1

Serialization using C#

December 2

2015 When we want to write or read data from files and database, we do not do this directly. Because object is not automatically converted. Casting is not useful here. So, that’s why we use serialization for doing it correctly.

Reading & Writing objects using serialization

Page 2: Serialization

Serialization using C#

By Salman Mushtaq Page 2

Asalam U Alaikum all readers!

I am Salman and today we discuss about Serialization in C#.

Suppose we have a small project. In project you develop a module and you need to save objects into

files. What can you do? Exactly you use some classes of System.IO like FileStream, StreamWriter and

StreamReader.

So, you make an object and write it to file. But when you see the file data it just Namespace.FileName.

You think what the wrong here is. Similarly when you try to read data you receive exception.

So here we need serialization. Serialization is used where we need to write object to database or file.

And vice versa. See the below diagram.

According to diagram. We have object and with the help of serialization object converts into bytes then

it saves to memory, database or file.

Now what we need to do all this.

- Class/Object for serialization

- A stream to serialized object

- Formatter

- Namespace: System.Runtime.Serialization

- Interface: ISerializable

Now, we write some code and explain it to you.

See code on start on next page.

Page 3: Serialization

Serialization using C#

By Salman Mushtaq Page 3

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace TestObjectWritingInTextfile { [Serializable()] class Worker : ISerializable { public string name; public string phone; // Default constructor public Worker() { name = null; phone = null; } // Parameterized constructor public Worker(string name, string phone) { this.name = name; this.phone = phone; } public Worker(SerializationInfo info, StreamingContext ctxt) { name = (string)info.GetValue("Name", typeof(string)); phone = (string)info.GetValue("Phone", typeof(string)); } public void GetObjectData(SerializationInfo info, StreamingContext ctxt) { info.AddValue("Name", name); info.AddValue("Phone", phone); } } public class Contact { public static void Main(String[] args) { Worker obj = new Worker("Salman Mushtaq", "03337465571"); //For writing data Stream sw = File.Open(@"C:\Users\Mani\Desktop\Files\names.txt", FileMode.Append); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(sw, obj); sw.Close(); //For reading data

Page 4: Serialization

Serialization using C#

By Salman Mushtaq Page 4

Stream sr = File.Open(@"C:\Users\Mani\Desktop\Files\names.txt", FileMode.Open); BinaryFormatter br = new BinaryFormatter(); obj = (Worker)br.Deserialize(sr); sr.Close(); Console.WriteLine("Name: " + obj.name); Console.WriteLine("Name: " + obj.phone); Console.ReadKey(); } } }

You need three namespaces:

using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary;

First of all you must implement ISerializable interface. It have two function one for Serialization and

second is for De-serialization. Both function have two parameters.

SerializationInfo info, StreamingContext ctxt

Method GetObjectdata is used for writing data. As we know we first convert all data into

bytes so we need formatter. That help us to write or read data on stream.

Hope you understand. If you have some question please email me at [email protected]