XmlSerialize
Introduction
The XmlSerialize and XmlDeserialize methods are methods which can be used to easily convert a class to an XML file, or an XML file back into an instance of a given type. XmlSerialize and XmlDeserialize are often used to save and load game data, such as player progress. Internally the FileManager uses the System.Xml.Serialization.XmlSerializer for serialization and deserialization. This means that any XML file created by the FileManager can be deserialized using the XmlSerializer if FlatRedBall is not available. Similarly, FlatRedBall can deserialize files created in other applications which use the XmlSerializer to save XML files. XmlSerialize uses the FileManager's RelativeDirectory when given relative paths to serialize.
Example
As mentioned above, game data can be saved to disk as XML files. For example, the following class could be used to save information about the user's progress:
A screen can be modified to save and load the project:
Customizing Serialization
The XmlSerialize internally uses XmlSerializer and XmlWriter classes. This means that all XML attributes (such as XmlIgnore) will apply normally.
XmlSerialize can create a directory
If the path of the file to save does not exist, then the XmlSerialize method will attempt to create the appropriate directory before saving the XML file.
"There was an error reflecting type"
The FileManager uses the internally. Therefore if the type cannot be serialized by the internal XmlSerializer then the FileManager.XmlSerialize will throw an exception. For information on this error, see this page.
Additional Information
Creating Save Classes - Discusses the "Save" class coding pattern commonly used to save game data.
Last updated