简体   繁体   English

具有内容管道的Monogame XML序列化

[英]Monogame XML serialization with Content pipeline

I've been trying to implement an XML system for items in my game.. but I just cant get it to work. 我一直在尝试为游戏中的项目实现XML系统。但是我无法使其正常工作。 I am using Monogame and the content pipeline that comes with it. 我正在使用Monogame及其随附的内容管道。 I've made an Inventory Class and an Item Class. 我做了一个库存类和一个项目类。

Here are the snippets from the Inventory Class (which would have the serialization): 以下是清单类的片段(将进行序列化):

public class Inventory
{    
    [XmlElement("Item")]
    public static List<Item> itemList;

    public Inventory (Vector2 _position)
    {
        itemList = new List<Item>();
    }

    public void LoadContent()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(Item));
        TextReader reader = new StreamReader("Content/Items/itemEntities.xml");
        object obj = deserializer.Deserialize(reader);
        Inventory XmlData = (Inventory)obj;
        reader.Close();
        }
}

And then I creaded an XML file: 然后,我读取了一个XML文件:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:ns="Microsoft.Xna.Framework">
  <Asset Type="Game.Item[]">
  <Item>
    <itemType>Weapon</itemType>
    <itemRarity>Rare</itemRarity>
    <itemID>0001</itemID>
    <positionID>
      <X>1</X>
      <Y>1</Y>
    </positionID>
    <name>The sword</name>
    <description>Description</description>
  </Item>
  <Item>
    <itemType>Equipment</itemType>
    <itemRarity>Uncommon</itemRarity>
    <itemID>0002</itemID>
    <positionID>
      <X>1</X>
      <Y>1</Y>
    </positionID>
    <name>The Item</name>
    <description>Description</description>
  </Item>
  <Item>
    <itemType>Drone</itemType>
    <itemRarity>Common</itemRarity>
    <itemID>0003</itemID>
    <positionID>
      <X>1</X>
      <Y>1</Y>
    </positionID>
    <name>The Drone</name>
    <description>Description</description>
  </Item>
  </Asset>
</XnaContent>

The problem now is that I get the following error: 现在的问题是出现以下错误:

error: Importer 'XmlImporter' had unexpected failure!
Microsoft.Xna.Framework.Content.Pipeline.InvalidContentException: Could not resolve type 'Game.Item[]'.

I have read on some other questions here that I would have to make a reference, but I just can't find a way to make that. 我在这里已经阅读了一些其他问题,需要参考一下,但是我找不到办法。 In the solution explorer I can see references, but when I click add, I dont see anything in Projects, only standard stuff like Frameworks etc. are there. 在解决方案资源管理器中,我可以看到参考,但是当我单击“添加”时,我在“项目”中看不到任何内容,只有诸如Frameworks等标准内容存在。 Oh and I have a constuctor with no arguments in Item class, so that should be fine. 哦,我在Item类中有一个没有参数的构造函数,所以应该没问题。

One more thing. 还有一件事。 I have tried to write the list of items into an XML file and it worked perfectly.. 我试图将项目列表写入XML文件,并且效果很好。

Thanks in advance! 提前致谢!

You need to add a reference to a compiled dll lib, which means, your types need to be defined in an external project. 您需要添加对已编译的dll库的引用,这意味着您的类型需要在外部项目中定义。

Create another project that compiles into dll, define all your structs and classes there (at least those that need serialization), and add reference to the dll from the content manager by doing the following: 创建另一个编译为dll的项目,在其中定义所有结构和类(至少需要序列化的结构和类),并通过执行以下操作从内容管理器中添加对dll的引用:

  1. open content pipeline manager. 打开内容管道管理器。
  2. select root node ("Content"). 选择根节点(“内容”)。
  3. On the properties tab click on "References". 在属性选项卡上,单击“参考”。
  4. Click on 'Add' and select the path to the dll file you generated from the other project. 单击“添加”,然后选择从其他项目生成的dll文件的路径。

I recently asked a similar question on the monogame community and currently its impossible to serialize types from your own project into / from xml without using another project that builds into a dll. 我最近在monogame社区问了一个类似的问题,目前,如果不使用另一个内置到dll中的项目,就不可能将您自己的项目中的类型序列化为xml或从xml中进行序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM