反序列化中间 XML 时出错.找不到类型 [英] Error while deserializing intermediate XML. Cannot find type

查看:37
本文介绍了反序列化中间 XML 时出错.找不到类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个错误信息在这里:

There was an error while deserializing intermediate XML. Cannot find type
"WindowsGame1.Tile".

我正在制作 XNA 游戏,现在我正在尝试从 xml 文件加载基于图块的关卡.现在,我的 xml 文件如下所示:

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>

我的 C# 文件如下所示:

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; }
        }
    }
}

我有一个类,它使用 XPath 库来解析 xml 文件并使 Tile 脱离它,但现在所有这些都被注释掉了,因为 xml 的东西无论如何都不起作用.在我的 Game1 类中,我尝试导入:

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;

并引用:

Tile tile;

这个 tile 类,根据我在尝试找到解决方案时发现的建议,但我仍然遇到相同的错误,并且我的游戏无法构建.任何人都知道我需要更改什么才能使这些 XML 工作正常工作?

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?

下面是解析 XML 的代码应该是什么样子,但目前,在我可以编译之前,所有代码都被注释掉了.

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);
            }
        }
    }
}

推荐答案

看起来您正在尝试使用 XNA 内容管道将 XML 文件构建为内容 - 所以我的回答将跳过您提到的所有 XPath 内容 -你不需要它.

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.

错误所指的反序列化"是指内容管道试图将您的 XML 转换为对象实例.然后它获取该对象并将其写出到 XNB 文件中.稍后,当您的游戏运行时,它会将该 XNB 文件加载回对象实例.

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.

哪个对象的实例?好吧,就您而言,是一个 WindowsGame1.Tile 对象.内容管道如何知道WindowsGame1.Tile 对象是什么?它没有 - 除非你告诉它 - 因此错误消息.

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.

你如何解决这个问题?

您需要从内容管道项目中引用包含 WindowsGame1.Tile 类型的程序集.

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).

因此,您必须将类型 WindowsGame1.Tile 放入从游戏项目和内容项目中引用的单独程序集(创建新的游戏库项目")(右键单击内容项目,添加引用,添加项目引用).

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");

<小时>

如果您在 XML 的 格式 中遇到另一个错误(我想我发现了一个错误,但我没有检查过),您可以通过创建一个Tile 类型的虚拟实例,然后使用 IntermediateSerializer.Serialize 生成适当的 XML.以下是如何执行此操作的示例.


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.

这篇关于反序列化中间 XML 时出错.找不到类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆