So This is my XML file:
Graphics/Backgrounds/background_0
And my XmlManager class:
public class XmlManager
{
public Type Type { get; private set; }
public T Load(string path)
{
T instance;
using (TextReader reader = new StreamReader(path))
{
XmlSerializer xml = new XmlSerializer(Type);
instance = (T)xml.Deserialize(reader);
}
return instance;
}
}
And my DataLoader class:
namespace ScreenLoader
{
public class DataLoader
{
public static string Path;
}
}
The compiler error:
Severity Code Description Project File Line Error There was an error while deserializing intermediate XML. 'Element' is an invalid XmlNodeType. Line 5, position 6.
So that's what I did: Added XNA Game Library ScreenLoader
to my project. Added the DataLoader class in it. Then I added reference to it in both my Content project and my Game project. Then I changed the game project to depend on the library. Then I adjusted the build order of the game project so that the library is built first. And I STILL CANNOT comprehend why the compiler complains like this. I just want to load content from XML files and have them compiled to XNB when I ship the game but I really need to know what I am missing. Thanks in advance!
Answer
You are trying to write code to deserialize your xml file. The Xml deserialization in Xna is automatic so you do not need your XmlManager class at all.
Add your Xml file (myXmlFile.xml) to your content project just like you would a texture or model. This will allow the build process to convert it from xml to serialized xnb. Now your project is aware of myXmlFile.xnb and when you load it using the Content Manager, it will automatically deserialize it for you. It was one of the really cool things about Xna so stick with it.
//In your Game1 class scope variables:
ScreenLoader.DataLoader myDataLoader;
//then in your LoadContent Method:
myDataLoader = Content.Load("myXmlFile");// it knows what a DataLoader is because you built that project first. the ("myXmlFile") refers to the myXmlFile.xnb, not the myXmlFile.xml.
Viola! myDataLoader.Path now holds "Graphics/Backgrounds/background_0"
here's a link: http://blogs.msdn.com/b/shawnhar/archive/2008/08/12/everything-you-ever-wanted-to-know-about-intermediateserializer.aspx
No comments:
Post a Comment