如何在XML中为XNA框架包含Vector2列表作为属性

时间:2013-02-16 15:40:40

标签: c# xml arrays xna

我有一个类,我想用它将数据导入到我用XNA创建的游戏中。它的基础是,我环顾四周并尝试了一些事情,但我无法正确编译:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="MyGameData.LevelXMLData">
  <LevelID>1</LevelID>
  <Path>
    <Item>1379 541</Item>
    <Item>1370 445</Item>
    <Item>1340 348</Item>
    <Item>1294 253</Item>
    <Item>1217 148</Item>
    <Item>1139 106</Item>
    <Item>1077 148</Item>
    <Item>1046 201</Item>
    <Item>1004 479</Item>
    <Item>999 633</Item>
    <Item>961 816</Item>
    <Item>917 918</Item>
    <Item>850 964</Item>
    <Item>824 939</Item>
    <Item>770 837</Item>
    <Item>738 673</Item>
    <Item>725 526</Item>
    <Item>549 520</Item>
  </Path>
  <WaveIDs>
    <Item>Wave1</Item>
    <Item>Wave2</Item>
    <Item>Wave3</Item>
  </WaveIDs>
  <WaveInterval>120000</WaveInterval>
  <TowerBuildSpots>
    <Item>1277 417 50 50</Item>
    <Item>1094 232 50 50</Item>
    <Item>956 207 50 50</Item>
    <Item>931 326 50 50</Item>
    <Item>1069 442 50 50</Item>
    <Item>906 566 50 50</Item>
    <Item>1056 589 50 50</Item>
    <Item>1031 722 50 50</Item>
    <Item>981 867 50 50</Item>
    <Item>824 854 50 50</Item>
    <Item>641 747 50 50</Item>
    <Item>774 639 50 50</Item>
    <Item>641 566 50 50</Item>
    <Item>691 442 50 50</Item>
  </TowerBuildSpots>
  </Asset>
</XnaContent>

“Path”是Vector2s的数组,“TowerBuildSpots”是一个Rectangles数组

Public Vector2[] Path;
Public Rectangle[] TowerBuildSpots;

我已经关注了我在集合中看到的其他人的代码示例,并且在我的Path标记中使用了标记,但是每当我尝试编译代码时,我都会收到以下错误

Error   1   There was an error while deserializing intermediate XML. 'Element' is an invalid XmlNodeType. Line 6, position 6.

这一行是文件中的第一行。

有谁可以请加入对这个问题的任何见解吗?这将是非常有帮助的。谢谢!

麦金太尔

编辑:我的反序列化代码在这里:

    public static LevelInfo Load(LevelXMLData data, ContentManager content)
    {
        int levelID = data.LevelID;

        string levelLocation = "Levels\\Level" + levelID + "\\";

        Waypoint[] path = new Waypoint[data.Path.Count()];
        WaveOfEnemies[] waves = new WaveOfEnemies[data.WaveIDs.Count()];

        TextureManager[] images = LevelImageInfo.Load(content.Load<LevelImageXMLData[]>(levelLocation + "ImageData"), content);

        // load waypoints
        for (int i = 0; i <= data.Path.Count() - 1; i++)
        {
            path[i] = new Waypoint(data.Path[i]);
        }

        for (int i = 0; i <= data.WaveIDs.Count() - 1; i++)
        {
            waves[i] = LoadWave(content.Load<WaveXMLData>(levelLocation + "Waves\\" + data.WaveIDs[i]));
        }

        return new LevelInfo(levelID, images, path, waves, data.WaveInterval, data.TowerBuildSpots);
    }

我的LevelXMLData结构在这里:

public struct LevelXMLData
{
    [ContentSerializer]
    public int LevelID;
    [ContentSerializer]
    public Vector2[] Path; 
    [ContentSerializer]
    public string[] WaveIDs;
    [ContentSerializer]
    public int WaveInterval;
    [ContentSerializer]
    public Rectangle[] TowerBuildSpots;
}

0 个答案:

没有答案