.NET Development Foundation/Serialization


Serialization and Input/Output


Serialization and Input/Output

edit

Exam objective: Implementing serialization and input/output functionality in a .NET Framework application

Topics

edit

Serialization

edit

Wikipedia's definition for serialization is : "in the context of data storage and transmission, serialization is the process of saving an object onto a storage medium (such as a file, or a memory buffer) or to transmit it across a network connection link in binary form".

The problem that is addressed here is that an object is created by a running process and is thus bound to the lifetime of that process instance. If for whatever reason, and there can be many, you want to "transport" the object in the context of another process instance you've got a problem, that you solve by "saving" the state of you object in the original process and "restoring" it in the destination process. This "saving" part is called serialization and the "restoring" part is called deserialization.

Serializable attribute

edit

An object is serializable if its class name is prefixed with the [Serializable] attribute.

Object Serialization

edit

One can use the BinaryFormatter class to serialize an object. To serialize, use the BinaryFormatter's Serialize() method which takes a stream and a serializable object as parameters. To deserialize, use the BinaryFormatter's Deserialize() method which takes a stream as a parameter and returns a object that can be cast back to the original object type. Remember to close streams after you use them by calling the stream's Close() method.

XML Serialization

edit

One can use the XmlSerializer class to serialize an object. To serialize, use the XmlSerializer's Serialize() method which takes a stream and a serializable object as parameters. To deserialize, use the XmlSerializer's Deserialize() method which takes a stream as a parameter and returns a object that can be cast back to the original object type. Remember to close streams after you use them by calling the stream's Close() method.

For an overview of XML and SOAP serialization see MSDN

Custom Serialization

edit

The ISerializable interface allows an object to control its own serialization and deserialization.

Readers

edit

Writers

edit

Formatters

edit

A formatter is used to serialize objects into streams.

Streams

edit

File IO

edit

Managing Byte Streams

edit

Compression

edit

Isolated storage

edit

For a general discussion on IsolatedStorage tasks see MSDN

Classes, Interfaces, and tools

edit

Serialize and deserialize

edit

Exam objective: Serialize or deserialize an object or an object graph by using runtime serialization techniques.

(Refer System.Runtime.Serialization namespace)

Serialization interfaces
edit

IDeserializationCallback interface - MSDN

IFormatter interface and IFormatterConverter interface

IFormatter interface - MSDN
IFormatterConverter interface - MSDN

ISerializable interface - MSDN

Serialization attributes
edit
For some serialization attributes exemple see MSDN

OnDeserializedAttribute class and OnDeserializingAttribute class

OnDeserializedAttribute class - MSDN
OnDeserializingAttribute class - MSDN

OnSerializedAttribute class and OnSerializingAttribute class

OnSerializedAttribute class - MSDN
OnSerializingAttribute class - MSDN

OptionalFieldAttribute class - MSDN

SerializationEntry structure and SerializationInfo class
edit

SerializationEntry structure - MSDN

SerializationInfo class - MSDN

ObjectManager class
edit

ObjectManager class - MSDN

Formatter class, FormatterConverter class, and FormatterServices class
edit

Formatter class - MSDN

FormatterConverter class - MSDN

FormatterServices class - MSDN

StreamingContext structure
edit

StreamingContext structure - MSDN

XML Serialization

edit

Exam objective: Control the serialization of an object into XML format by using the System.Xml.Serialization namespace.

XmlSerializer class - MSDN

Exam objective: Serialize and deserialize objects into XML format by using the XmlSerializer class

Control serialization by using serialization attributes - MSDN

For a list of attributes for controlling serialization see MSDN

Implement XML Serialization interfaces to provide custom formatting for XML serialization - MSDN

Delegates and event handlers are provided by the System.Xml.Serialization namespace - MSDN

Custom serialization

edit

Exam objective: Implement custom serialization formatting by using the Serialization Formatter classes.

SoapFormatter class - MSDN

(Refer System.Runtime.Serialization.Formatters.Soap namespace)

BinaryFormatter class - MSDN

(Refer System.Runtime.Serialization.Formatters.Binary namespace

File system classes

edit

Exam objective: Access files and folders by using the File System classes.

(Refer System.IO namespace)

File class and FileInfo class

For common IO tasks see MSDN
File class - MSDN
FileInfo class - MSDN

Directory class and DirectoryInfo class

Directory class - MSDN
DirectoryInfo class - MSDN

DriveInfo class and DriveType enumeration

DriveInfo class - MSDN
DriveType enumeration - MSDN

FileSystemInfo class and FileSystemWatcher class

FileSystemInfo class
FileSystemWatcher class
The FileSystemWatcher class is designed to detected changes in the filesystem.
It can be parametrised with the Filter and Path Property.
  Example: 
  FileSystemWatcher w = new FileSystemWatcher();
  w.Filter = "*.txt";
  w.Path = @"C:\Windows";
The Filter property is only used to check the pattern of a file name. So do not use a directory path there.
You can add methods such as the WaitForChanged(..) to watch for changes in the specified area.

Path class - MSDN

The System.IO.Path class has many useful static methods for creating and parsing resource paths.

ErrorEventArgs class and ErrorEventHandler delegate

ErrorEventArgs class - MSDN
ErrorEventHandler delegate - MSDN

RenamedEventArgs class and RenamedEventHandler delegate

RenamedEventArgs class - MSDN
RenamedEventHandler delegate - MSDN

Byte streams

edit

Exam objective: Manage byte streams by using Stream classes.

(Refer System.IO namespace)

FileStream class - MSDN

Stream class - MSDN

System.IO.Stream is the abstract base class that all other streams inherit from. It is not possible in instantiate a Stream class. Instead use one of the other classes that derive from Stream.
In terms of the 70-536 exam objectives, the most important classes that inherit from Stream are:
  • System.IO.FileStream
  • System.IO.MemoryStream
  • System.IO.Compression.DeflateStream
  • System.IO.Compression.GZipStream
  • System.Security.Cryptography.CryptoStream
  • System.IO.BufferedStream
For a complete list of classes that inherit from Stream see MSDN.
For a discussion on File and Stream IO see MSDN.

MemoryStream class - MSDN

BufferedStream class - MSDN

Reader and Writer classes

edit

Exam objective: Manage the .NET Framework application data by using Reader and Writer classes.

(Refer System.IO namespace)

StringReader class and StringWriter class - MSDN and MSDN

StringReader and StringWriter inherit from TextReader/TextWriter.
  • StringReader is a TextReader for strings.
  • StringWriter is a TextWriter for strings.

TextReader class and TextWriter class

TextReader class - MSDN
TextReader and TextWriter are abstract base classes that StreamReader, StreamWriter, StringReader, and StringWriter derive from. StreamReader and StringReader derive from TextReader. StreamWriter and StringWriter derive from TextWriter.
TextWriter class - MSDN

StreamReader class and StreamWriter class - MSDN and MSDN

The StreamReader and StreamWriter classes provide basic functionality for reading and writing to character-based streams (ReadLine(), WriteLine(), ReadToEnd()).
StreamReader and StreamWriter inherit from the abstract classes TextReader and TextWriter:
  • StreamReader is a TextReader of streams.
  • StreamWriter is a TextWriter of streams.
The Peek and the Read method of a StreamReader:
  • The Peek-method gets the character at a certain position, but does not advance.
  • The Read-method gets the character at a certain position and advances.

BinaryReader class and BinaryWriter class

BinaryReader class - MSDN
BinaryWriter class - MSDN

Compression and isolated storage

edit

Exam objective: Compress or decompress stream information in a .NET Framework application and improve the security of application data by using isolated storage.

(Refer System.IO.Compression namespace)

(Refer System.IO.IsolatedStorage namespace)

IsolatedStorageFile class - MSDN

IsolatedStorageFileStream class - MSDN

DeflateStream class - MSDN

GZipStream class - MSDN


Previous / Next