Serializing Object Graphs as XML
Learn about object graph serialization, enabling the conversion of live object graphs to and from bytes for storage and retrieval.
An object graph is multiple objects related to each other either through a direct reference or indirectly through a chain of references.
Serialization
Serialization converts a live object graph into a sequence of bytes using a specified format. Deserialization is the reverse process.
We would do this to save the current state of a live object so that we can recreate it in the future, for example, saving the current state of a game so that we can continue at the same place tomorrow. Serialized objects are usually stored in a file or database. We can specify dozens of formats, but eXtensible Markup Language (XML) and JavaScript Object Notation (JSON) are the most common.
.NET has multiple classes that will serialize to and from XML and JSON. We will start by looking at XmlSerializer
and JsonSerializer
.
Serializing as XML
Let’s start by looking at XML, probably the world’s most used serialization format (for now). To show a typical example, we will define a custom class ...