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.
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.
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
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.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.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:
<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");
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.
08/23/2011 - 04:16
Did you find a solution?
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.
11/28/2011 - 13:29
YOU HAVE TOTALLY SAVED MY LIFE!!!!!!
GREAT TUTORIAL!!!!!!
12/04/2011 - 10:41
You did an excellent job <3
Add Comment
[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.