.NET Development Foundation/Serialization
- 1 Serialization and Input/Output
- 1.1 Topics
- 1.2 Classes, Interfaces, and tools
- 1.2.1 Serialize and deserialize
- 1.2.2 XML Serialization
- 1.2.3 Custom serialization
- 1.2.4 File system classes
- 1.2.5 Byte streams
- 1.2.6 Reader and Writer classes
- 1.2.7 Compression and isolated storage
Serialization and Input/OutputEdit
Exam objective: Implementing serialization and input/output functionality in a .NET Framework application
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.
An object is serializable if its class name is prefixed with the [Serializable] attribute.
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.
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
The ISerializable interface allows an object to control its own serialization and deserialization.
A formatter is used to serialize objects into streams.
Managing Byte StreamsEdit
For a general discussion on IsolatedStorage tasks see MSDN
Classes, Interfaces, and toolsEdit
Serialize and deserializeEdit
Exam objective: Serialize or deserialize an object or an object graph by using runtime serialization techniques.
(Refer System.Runtime.Serialization namespace)
IDeserializationCallback interface - MSDN
IFormatter interface and IFormatterConverter interface
- IFormatter interface - MSDN
- IFormatterConverter interface - MSDN
ISerializable interface - MSDN
- 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 classEdit
SerializationEntry structure - MSDN
SerializationInfo class - MSDN
ObjectManager class - MSDN
Formatter class, FormatterConverter class, and FormatterServices classEdit
Formatter class - MSDN
FormatterConverter class - MSDN
FormatterServices class - MSDN
StreamingContext structure - MSDN
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
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 classesEdit
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
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:
- 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 classesEdit
Exam objective: Manage the .NET Framework application data by using Reader and Writer classes.
(Refer System.IO namespace)
- 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 dervive from TextWriter.
- TextWriter class - 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 storageEdit
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