How to create a custom XML File?
For complex game data, using a software tool to create and maintain these assets may be useful. Level tables, for example, might be easier to develop through a custom-level editor tool.
The IntermediateSerializer class of the MonoGame Framework can be employed by custom tools running under Windows to directly serialize game data to an XML file.
An XML file generated in this way then can be included in the game project and imported by XmlImporter Class as part of the Content Pipeline. Because XmlImporter is actually a wrapper for IntermediateSerializer, it is certain that the XML file will be in the correct format to be deserialized by the same facility.
The IntermediateSerializer class is controlled through the XmlWriter class of the .NET Framework defined in System.Xml. The properties of the XmlWriterSettings class can be used to specify its output properties.
The serializer produces its output according to these rules:
- All public fields and properties are serialized; a separate XML element is used for each.
- Protected, private, or internal data is skipped.
- Get-only or set-only properties are skipped.
- Properties come before fields.
- If there is more than one field or property, these are serialized in the order they are declared.
- Nested types are serialized using nested XML elements.
- When the class derives from another, members of the base class are serialized before data from the derived type.
XML Serialization Example
The following steps create a simple program that demonstrates how a program can use the IntermediateSerializer method IntermediateSerializer.Serialize Generic Method to serialize program data to an XML file.
Step 1: Create a New Project
You will create a new project in Visual Studio.
On the
Filemenu, clickNew, and then clickProject.In the
New Projectdialog box, ensureWindowsis selected in theProject typespane, and then clickConsole Applicationin theTemplatespane.Open the new project, then in the
Solution Explorer, right-click theDependenciesfolder, and then clickManage NuGet Packages...Click on the
Browsetab (if not selected) and search for theMonoGame.Framework.Content.Pipelinepackage, and then clickInstall.Accept any dialog prompts for Licenses or dependencies as they appear.
Additionally, install the
MonoGame.Framework.DesktopGLNuGet package which is required by theContent.Pipelinepackage.
Step 2: Add XML Serialization
In the
Solution Explorer, double-clickProgram.csto edit it.Add
usingdeclarations for the System.Xml and Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate namespaces.using System.Xml; using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;Define a class to serialize to XML with the properties you wish to store in your custom XML.
namespace XMLSerializerTest { class MyData { public int elf = 23; public string hello = "Hello World"; } }Within the function
Mainof theProgramclass, add the following code. This code employs IntermediateSerializer.Serialize Generic Method to serialize theMyDataclass as an XML file.
class Program
{
static void Main(string[] args)
{
MyData ExampleData = new MyData();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create("example.xml", settings))
{
IntermediateSerializer.Serialize(writer, ExampleData, null);
}
}
}
Step 3: Generate XML
- Press F5 to build and execute the program.
- Examine the
example.xmlfile in the project'sbin\\Debug\\netx.0folder.
Note
netx.0 relates to the version of .NET your project is building under, net6.0 for .NET 6 and net8.0 for .NET 8
You should have an output that looks like the following:
<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:XMLSerializerTest="XMLSerializerTest">
<Asset Type="XMLSerializerTest:MyData">
<elf>23</elf>
<hello>Hello World</hello>
</Asset>
</XnaContent>