Serialize Public Members of a Nonserializable Object

Problem

You want to serialize an object that isn’t marked with the Serializable attribute.

Solution

Use the System.Xml.Serialization.XmlSerializer class, which provides a more limited form of serialization.

Discussion

The XmlSerializer class allows you to serialize an object that isn’t explicitly marked as serializable. This is especially convenient if you need to store state for an object and you aren’t able to modify the class code to make it serializable (perhaps because you don’t have access to the source code).

The XmlSerializer class works much like the binary and SOAP formatters described in recipe Serialize an Object to Disk, but with several limitations. First of all, it doesn’t store any ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.