Loading XML Through XNA Content Pipeline

Skill

Loading XML Through XNA Content Pipeline

Posted in:

XNA is quite possibly one of the most robust game programming frameworks out there. You can do nearly anything with it, and one of the reasons it is so flexible is its "Content Pipeline". But sometimes it can be a bit tricky to use this feature, requiring us to do bit more work. But in the end, it helps to use the Content Pipeline. Luckily today we are going over how to use it to load in some XML.

A quick google search will reveal that loading in XML with C# and XNA can be a bit more complicated that it seems. There are several ways to accomplish XML integration, all dependent on your needs. Today we are going to go over the simplest way to load XML in XNA using the fancy Content Pipeline.

For the best viewing, use full screen mode.

Get Adobe Flash player
Anonymous12312312
08/05/2010 - 17:44

I followed this to a T and I get a run-time error that the compiler cannot find the xml file.

reply

Sam
11/22/2010 - 20:26

Hi. This is a great video, except there are problems with XNA 4.0. I'll post a fix when I find out what needs to be changed

reply

CrazySilence
01/23/2011 - 05:53

great video and tutorial but am facing a huge problem when i try using it with texture2d
here's my classes and my xml file

class1:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Test
{
    [ContentSerializerRuntimeType("Test.Map, Test")]
    public class MapContent
    {
        public string TextureName { get; set; }
        public Vector2 Position { get; set; }
    }
}

class2:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Test
{
    public class Map
    {
        GraphicsDeviceManager graphic;
        Game game1 = new Game();
        ContentManager content;
        GraphicsDevice gdevice;

        public Map()
        {
            graphic = new GraphicsDeviceManager(game1);
            content = new ContentManager(game1.Services, "Content");
            gdevice = graphic.GraphicsDevice;
        }

        [ContentSerializer]
        private string TextureName
        {
            get { throw new NotSupportedException(); }
            set { content.Load<Texture2D>(value); }
        }

        [ContentSerializerIgnore]
        public Texture2D Texture { get; private set; }

        public Vector2 Position { get; set; }
    }
}

xml file:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
        <Asset Type="Test.MapContent">
                <TextureName>2</TextureName>
                <Position>0 0</Position>
        </Asset>
</XnaContent>

in game1 file i add these lines
Map test = new Map();
test = Content.Load("Test1");

reply

CrazySilence
01/24/2011 - 18:40

sorry i forgot to mention the problem
when i run the code i get an exception here
set { content.Load(value); }
Error loading "2". GraphicsDevice component not found.

reply

Anonymous
08/23/2011 - 04:16

Did you find a solution?

reply

Josh Postema
04/11/2011 - 13:42

This is by far the most helpful tutorial I've found on the subject. Thank you very much!

I have it working perfectly in XNA 4.0.

reply

Dacit
11/28/2011 - 13:29

YOU HAVE TOTALLY SAVED MY LIFE!!!!!!
GREAT TUTORIAL!!!!!!

reply

Anonymous
12/04/2011 - 10:41

You did an excellent job <3

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.