Simple Deserialization with XML and XNA Content Pipeline

The XNA Content Pipeline can at first seem daunting but is extremely easy to use once you get the hang of it. I wanted to share a small class that I created to represent an enemy entity that has its information stored in an XML format. The content pipeline reads my XML animated sprite data and imports/processes it to a .xnb file at build time. At run time, this .xnb file is then deserialized into an object that I can use to draw to the screen.

My XML content looks like this:

<?xml version="1.0" encoding="utf-8" ?>
    <Asset Type="ContentPipelineExtensionLibrary.EntityData">

The C# class associated with this data at run time looks like this:

namespace ContentPipelineExtensionLibrary
    public class EntityData
        public int CollisionOffset;

        public float HorizontalSpeed;

        public float VerticalSpeed;

        public String SpriteIdle;

        public String SpriteMove;

The XNA Content Pipeline has a built in importer and processor for XML –> Object deserialization. As long as you have the “Type” attribute of the “Asset” element set to the correct namespace and class, the Content Pipeline will take care of the rest. Just load this content like so:

EntityData enemy = content.Load<EntityData>("SimpleEnemy");