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
File
menu, clickNew
, and then clickProject
.In the
New Project
dialog box, ensureWindows
is selected in theProject types
pane, and then clickConsole Application
in theTemplates
pane.Open the new project, then in the
Solution Explorer
, right-click theDependencies
folder, and then clickManage NuGet Packages..
.Click on the
Browse
tab (if not selected) and search for theMonoGame.Framework.Content.Pipeline
package, and then clickInstall
.Accept any dialog prompts for Licenses or dependencies as they appear.
Additionally, install the
MonoGame.Framework.DesktopGL
NuGet package which is required by theContent.Pipeline
package.
Step 2: Add XML Serialization
In the
Solution Explorer
, double-clickProgram.cs
to edit it.Add
using
declarations 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
Main
of theProgram
class, add the following code. This code employs IntermediateSerializer.Serialize Generic Method to serialize theMyData
class 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.xml
file in the project'sbin\\Debug\\netx.0
folder.
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>