> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/io/filemanager/xmlserialize.md).

# 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](/flatredball/api/flatredball/io/filemanager/relativedirectory.md) 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:

```lang:c#
public class ProfileSaveData
{
    public string Name { get; set; }
    public int Experience { get; set; }
    public int Gold { get; set; }
}
```

A screen can be modified to save and load the project:

```lang:c#
void CustomActivity(bool firstTimeCalled)
{
    if(InputManager.Keyboard.KeyPushed(Keys.S))
    {
        PerformSave();
    }
    if(InputManager.Keyboard.KeyPushed(Keys.L))
    {
        PerformLoad();
    }

}

private void PerformSave()
{
    string fileName = "PlayerSave.xml";

    // Normally you'd have the profile stored somewhere, but 
    // we'll just create one here
    var saveData = new DataTypes.ProfileSaveData();

    saveData.Name = "Cecil";
    saveData.Gold = 3000;
    saveData.Experience = 500;

    FlatRedBall.IO.FileManager.XmlSerialize(saveData, fileName);

    FlatRedBall.Debugging.Debugger.CommandLineWrite("Saved " + fileName);

}

private void PerformLoad()
{
    string fileName = "PlayerSave.xml";

    var doesFileExist = FlatRedBall.IO.FileManager.FileExists(fileName);

    if(doesFileExist)
    {
        var saveData = FlatRedBall.IO.FileManager.XmlDeserialize<DataTypes.ProfileSaveData>(fileName);

        FlatRedBall.Debugging.Debugger.CommandLineWrite("Loaded file!");
        FlatRedBall.Debugging.Debugger.CommandLineWrite("Name:" + saveData.Name);
        FlatRedBall.Debugging.Debugger.CommandLineWrite("Gold:" + saveData.Gold);
        FlatRedBall.Debugging.Debugger.CommandLineWrite("Experience:" + saveData.Experience);
    }
    else
    {
        FlatRedBall.Debugging.Debugger.CommandLineWrite("Could not find file " + fileName);
    }

}
```

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-2976eb6d553e0b8dde148fd9fdb21f640b9dbbf0%2F2017-02-img_58913fffc58ee.png?alt=media)

### Customizing Serialization

The XmlSerialize internally uses [XmlSerializer](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx) and [XmlWriter](http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx) classes. This means that all XML attributes (such as [XmlIgnore](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlignore.aspx)) 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](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx|XmlSerializer). 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](http://stackoverflow.com/questions/60573/xmlserializer-there-was-an-error-reflecting-type).

### Additional Information

* [Creating Save Classes](https://github.com/flatredball/FlatRedBallDocs/tree/main/api/flatredball/io/filemanager/broken-reference/README.md) - Discusses the "Save" class coding pattern commonly used to save game data.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/io/filemanager/xmlserialize.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
