问题
The whole error message is here:
There was an error while deserializing intermediate XML. Cannot find type
"WindowsGame1.Tile".
I'm making an XNA game, and right now I'm trying to load in a tile-based level from an xml file. Right now, my xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
<Asset Type="WindowsGame1.Tile">
<Tile>
<X>0</X>
<Y>0</Y>
<ImageName>grass-tile</ImageName>
</Tile>
</Asset>
</XnaContent>
And my C# file looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace WindowsGame1
{
public class Tile
{
public int x;
public int y;
public string imageName;
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public string ImageName
{
get { return imageName; }
set { imageName = value; }
}
}
}
I have a class that uses the XPath libraries to parse the xml file and make Tile's out of it, but it's all commented out right now because the xml stuff doesn't work anyways. In my Game1 class I tried importing:
using WindowsGame1.Tile;
and referencing:
Tile tile;
This tile class, as per a suggestion I found while trying to find a solution to this, but I'm still getting the same error, and my game won't build. Anyone know what I need to change to get this XML stuff working?
Here's what the code that should be parsing the XML should look like, though at the moment, it's all commented out until I can get it to compile.
namespace WindowsGame1
{
class ActionScreen : GameScreen
{
public ActionScreen(Game game, SpriteBatch spriteBatch, String file, AnimatedSprite sprite)
: base(game, spriteBatch)
{
this.file = file;
this.sprite = sprite;
initializeTiles();
}
private void initializeTiles()
{
XPathDocument doc = new XPathDocument(file);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = nav.Select("tile");
while (iter.MoveNext())
{
int x, y;
string fileName;
x = int.Parse(iter.Current.GetAttribute("X", ""));
y = int.Parse(iter.Current.GetAttribute("Y", ""));
fileName = iter.Current.GetAttribute("ImageName", "");
Console.WriteLine(x + " " + y + " " + fileName);
}
}
}
}
回答1:
It looks like you're trying to use the XNA content pipeline to build the XML file as content - so my answer will skip all the XPath stuff you metioned - you don't need it.
The "deserializing" that the error is referring to is when the content pipeline is trying to convert your XML into an object instance. It then takes that object and writes it out to an XNB file. Later on, when your game runs it will load that XNB file back into an object instance.
An instance of which object? Well, in your case, a WindowsGame1.Tile
object. How does the content pipeline know what a WindowsGame1.Tile
object is? It doesn't - not unless you tell it about it - hence the error message.
How do you fix this?
You need to reference the assembly that contains the type WindowsGame1.Tile
from your content pipeline project.
You can't reference your game project directly - because that would create a circular dependency (your game project already depends on the content project).
Therefore you must put the type WindowsGame1.Tile
into a separate assembly (create a new "Game Library Project") that you reference from both your game project and your content project (right click the content project, add reference, add project reference).
Then you can load your tile in-game via the content manager:
Tile myTile = Content.Load<Tile>("assetName");
If you get another error about the format of your XML (I think I spy an error, but I haven't checked), you can figure out what it is supposed to be by creating a dummy instance of your Tile
type, then use IntermediateSerializer.Serialize
to generate the appropriate XML. Here is an example of how to do this.
回答2:
To make it deserialize properly, you have to have your properties names same in XML and in actual class. Try to rename property "imageName" to "image" in your Tile class.
回答3:
The issue is you are using GetAttribute()
method instead doing a Select("X").Value
. For example, instead of
x = int.Parse(iter.Current.GetAttribute("X", ""));
try
x = int.Parse(iter.Current.Select("X").Value, string.Empty));
You also may want to consider the System.Xml.Serlization.XmlSerializer class since it is easier to use but I do not know whether it is included in the version of the XNA framework you are using.
来源:https://stackoverflow.com/questions/12239785/error-while-deserializing-intermediate-xml-cannot-find-type