32
1

Dotnet IO And Serialization

Embed Size (px)

DESCRIPTION

Dotnet IO And Serialization

Citation preview

Page 1: Dotnet IO And Serialization

1

Page 2: Dotnet IO And Serialization

2

Page 3: Dotnet IO And Serialization

3

Page 4: Dotnet IO And Serialization

4

Page 5: Dotnet IO And Serialization

5

Page 6: Dotnet IO And Serialization

6

Page 7: Dotnet IO And Serialization

7

Page 8: Dotnet IO And Serialization

8

Page 9: Dotnet IO And Serialization

9

Page 10: Dotnet IO And Serialization

10

Page 11: Dotnet IO And Serialization

11

Page 12: Dotnet IO And Serialization

12

Page 13: Dotnet IO And Serialization

13

Page 14: Dotnet IO And Serialization

14

Page 15: Dotnet IO And Serialization

15

Page 16: Dotnet IO And Serialization

1. Every class object, by default , can’t be serialized

2. CLR has to provide support fir serialization

3. So, the class, whose object you would like to serialize has to be marked with [Serializable] attribute.

4. If you do not want some members to be serialized, then mark them with [NonSerialized] attribute. So, every other member will be serialized except those which are with that attribute.

16

Page 17: Dotnet IO And Serialization

1. Consider, Car class has an object of Radio class, which available to derived classes of Car class too.

2. Maruti class is derived from Car class

3. So, Car and Maruti classes are bound through “IS-A” relationship.

4. Since, Maruti is derived from Car class, then Maruti class also posses the object of Radio class.

5. So, Car and Maruti class, both, is bound with Radio class with “HAS-A” relationship.

6. So, Car, Maruti and Radio class objects form an object graph.

7. If, you want to serialize an object of Maruti class, then Car as well as Radio class, both have to be marked with [Serializable] attribute.

8. Otherwise, Maruti class members will be serialized, but not the members of other two classes.

17

Page 18: Dotnet IO And Serialization

18

Page 19: Dotnet IO And Serialization

1. You have to add a reference to System.Runtime.Serialization.Formatters.Soap.dll(version 2.0) in your application to avail SoapFormatter class for Soap Serialization

2. In other serialization cases, there is no need to add any reference to any libraries.

19

Page 20: Dotnet IO And Serialization

20

Page 21: Dotnet IO And Serialization

1. All types of fields are serialized, even if it is private

2. Assembly metadata of the containing assembly is also serialized

3. Class information is serialized

4. If the class is part of an object graph, then information about all the classes, part of that object graph will be serialized, along with their containing assembly metadata

5. Data of the fields will be serialized being associated with the field name

6. Object of class with any (public or internal) access specifier is only serialized

Example:

Serialization:

static void Main(string[] args)

{

Radio radioobject = new Radio();

radioobject.HasFM = true;

radioobject.Made = "Philips";

Maruti marutiobject = new Maruti();

marutiobject.Color = "Black";

marutiobject.HasAc = false;

marutiobject.Radio = radioobject;

FileStream fs = new FileStream(@"C:\cardata.bin", FileMode.OpenOrCreate);

BinaryFormatter binaryFormatterobject = new BinaryFormatter();

binaryFormatterobject.Serialize(fs, marutiobject);

}

DeSerialization:

static void Main(string[] args)

{

FileStream fs = new FileStream(@"C:\cardata.bin", FileMode.OpenOrCreate);

BinaryFormatter binaryFormatterobject = new BinaryFormatter();

object obj = binaryFormatterobject.Deserialize(fs);

Maruti marutiobject = (Maruti)obj;

Console.WriteLine("Color: " + marutiobject.Color);

Console.WriteLine("AC: " + marutiobject.HasAc);

Console.WriteLine("Radio FM: " + marutiobject.Radio.HasFM);

Console.WriteLine("Radio Brand: " + marutiobject.Radio.Made);

}

21

Page 22: Dotnet IO And Serialization

1. All types of fields are serialized, even if it is private

2. Assembly metadata of the containing assembly is also serialized

3. Class information is serialized

4. If the class is part of an object graph, then information about all the classes, part of that object graph will be serialized, along with their containing assembly metadata

5. Data of the fields will be serialized being associated with the field name

6. Object of class with any (public or internal) access specifier is only serialized

Example:

Serialization:

static void Main(string[] args)

{

Radio radioobject = new Radio();

radioobject.HasFM = true;

radioobject.Made = "Philips";

Maruti marutiobject = new Maruti();

marutiobject.Color = "Black";

marutiobject.HasAc = false;

marutiobject.Radio = radioobject;

FileStream fs = new FileStream(@"C:\cardata.soap", FileMode.OpenOrCreate);

SoapFormatter soapFormatterobject = new SoapFormatter();

soapFormatterobject.Serialize(fs, marutiobject);

}

DeSerialization:

static void Main(string[] args)

{

FileStream fs = new FileStream(@"C:\cardata.soap", FileMode.OpenOrCreate);

SoapFormatter soapFormatterobject = new SoapFormatter();

object obj = soapFormatterobject.Deserialize(fs);

Maruti marutiobject = (Maruti)obj;

Console.WriteLine("Color: " + marutiobject.Color);

Console.WriteLine("AC: " + marutiobject.HasAc);

Console.WriteLine("Radio FM: " + marutiobject.Radio.HasFM);

Console.WriteLine("Radio Brand: " + marutiobject.Radio.Made);

}

22

Page 23: Dotnet IO And Serialization

1. All types of fields are not serialized. XML serialization converts (serializes) only the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document

2. XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport

3. Assembly metadata of the containing assembly is not serialized

4. Field data gets serialized being associated with the property name or public field name

5. Object of class with ‘public’ access specifier is only serialized

6. If the class is part of an object graph, then information about all the classes, part of that object graph will be serialized.

7. The following items can be serialized using the XMLSerializable class:

Public read/write properties and fields of public classes

Classes that implement ICollection or IEnumerable (Note that only collections are serialized, not public properties)

XmlElement objects

XmlNode objects

DataSet objects

Example:

Serialization:

static void Main(string[] args)

{

Radio radioobject = new Radio();

radioobject.HasFM = true;

radioobject.Made = "Philips";

Maruti marutiobject = new Maruti();

marutiobject.Color = "Black";

marutiobject.HasAc = false;

marutiobject.Radio = radioobject;

FileStream fs = new FileStream(@"C:\cardata.xml", FileMode.OpenOrCreate);

Type[] extratypesinfo = new Type[] { typeof(Car), typeof(Radio) };

XmlSerializer xmlFormatterobject = new XmlSerializer(typeof(Maruti), extratypesinfo);

xmlFormatterobject.Serialize(fs, marutiobject);

}

DeSerialization:

static void Main(string[] args)

{

FileStream fs = new FileStream(@"C:\cardata.xml", FileMode.OpenOrCreate);

Type[] extratypesinfo = new Type[] { typeof(Car), typeof(Radio) };

XmlSerializer xmlFormatterobject = new XmlSerializer(typeof(Maruti), extratypesinfo);

object obj = xmlFormatterobject.Deserialize(fs);

Maruti marutiobject = (Maruti)obj;

Console.WriteLine("Color: " + marutiobject.Color);

Console.WriteLine("AC: " + marutiobject.HasAc);

Console.WriteLine("Radio FM: " + marutiobject.Radio.HasFM);

Console.WriteLine("Radio Brand: " + marutiobject.Radio.Made);

}

23

Page 24: Dotnet IO And Serialization

24

Page 25: Dotnet IO And Serialization

25

Page 26: Dotnet IO And Serialization

26

Page 27: Dotnet IO And Serialization

27

Page 28: Dotnet IO And Serialization

28

Page 29: Dotnet IO And Serialization

29

Page 30: Dotnet IO And Serialization

30

Page 31: Dotnet IO And Serialization

Reference

31

Page 32: Dotnet IO And Serialization

32