You have seen how the various collections store items, how to access their elements, and even how to sort and search the collections. To make the most of the collections and to use them to store large sets of data, you should also learn how to persist collections to disk files.
Persisting data means to store them on disk at the end of one session and reload them into the same application in a later session. The file with the persisted data can also be shared among different applications and even different computers, as long as there’s an application that knows what to do with the data. What good is it to create a large collection if your application can’t save it and retrieve it from a disk file in another session?
Since time immemorial, programmers had to write code to save their data to disk. In this chapter, you’ll see how to convert objects to streams with a technique known as serialization, which is the process of converting arbitrary objects to streams of bytes. After you obtain the serialized stream for a specific object, you can persist the object to disk, as well as read it back. The process of reconstructing an object from its serialized form is called deserialization. Together, serialization and deserialization allow you to store objects of any type to disk files and reuse them at a later session. Serialization is not limited to saving data to files, however. You can serialize objects to any stream, including a memory stream, a network stream, or even a cryptographic stream that emits encrypted data.
There are three types of serialization: binary serialization, SOAP (Simple Object Access Protocol or Service Oriented Architecture Protocol) serialization, and XML (Extensible Markup Language) serialization. Binary and SOAP serialization are very similar; XML serialization is a little different, but it allows you to customize the serialization process.
Binary serialization is performed with the BinaryFormatter class, and it converts the values of the object’s properties into a binary stream. The result of the binary serialization is compact and efficient. However, binary-serialized objects can be used only by applications that have access to the class that produced the objects and can’t be used outside .NET. Another limitation of binary serialization is that the output it produces is not human readable, and you can’t do much with a file that contains a binary serialized object without access to the original application’s code. Because binary serialization is very compact and very efficient, it’s used almost exclusively to persist objects between sessions of an application or between applications that share the same classes. For example, you can create an ArrayList of Person objects, serialize them to a file, and reload the collection of the serialized objects from the file at a later time or another session. SOAP serialization produces a SOAP-compliant envelope that describes its contents and serializes the objects in SOAP-compliant format. SOAP-serialized data is suitable for transmission to any system that understands SOAP, and it’s implemented by the SoapFormatter class. Unlike binary serialization, SOAP-serialized data are firewall friendly, and SOAP serialization is used to remote objects to a server on a different domain.
XML serialization, which is implemented by the XmlSerializer class, is a different type of serialization. The XmlSerializer class serializes public, read-write properties only. Because it doesn’t serialize the private members or read-only properties, XmlSerializer doesn’t quite preserve the state of the object. Another limitation of XmlSerializer is that it doesn’t serialize collections, with the exception of arrays and ArrayLists. However, it can be customized with the use of attributes (special keywords that prefix the members of a class), and it’s as close as we can get to a universal data-exchange format. As you will see at the end of this chapter, you can open the file generated by XmlSerializer with an XML editor and not only view, but also edit it. In the following chapter, you will learn how to query the contents of an XML file.