0
votes

I have a combined XNA-Silverlight project that I am developing, and I am trying to add XML content to it. Before this, I had the following setup:

  1. Content Project
  2. XNA Game Library - references the content project
  3. Silverlight Project - references the XNA game library

I had images in the content project, and I could compile and load them just fine from the Silverlight code. Now I am trying to add XML content to the content project and code to the game library that describes it, but I must be doing something wrong because I get the following error: There was an error while deserializing intermediate XML. Cannot find type "CrystalLib.Map"

In the content project I have a folder called maps, and under it I have the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
  <Asset Type="CrystalLib.Map">
    <TileSetFile>grassland</TileSetFile>
    <Dimensions>500 250</Dimensions>
    <Tiles>
      ... Lots of integers (500 x 250)
    </Tiles>
  </Asset>
</XnaContent>

Then I have the following class in the xna game project (Map.cs):

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace CrystalLib
{
    /// Represents the data stored in a map file
    class Map
    {
        /// The name of tile set file
        private string tileSetFile;

        /// The name of tile set file
        public string TileSetFile
        {
            get { return tileSetFile; }
            set { tileSetFile = value; }
        }

        /// The dimensions of the map, in tiles.
        private Point dimensions;

        /// The dimensions of the map, in tiles.
        public Point Dimensions
        {
            get { return dimensions; }
            set { dimensions = value; }
        }

        /// Spatial array for the tiles for this map.
        private int[] tiles;

        /// Spatial array for the tiles for this map.
        public int[] Tiles
        {
            get { return tiles; }
            set { tiles = value; }
        }

        /// Retrieves the base layer value for the given map position.
        public int GetTile(int x, int y)
        {
            return tiles[y * dimensions.X + x];
        }

        /// Read a Map object from the content pipeline.
        public class MapReader : ContentTypeReader<Map>
        {
            protected override Map Read(ContentReader input, Map existingInstance)
            {
                Map map = existingInstance;
                if (map == null)
                {
                    map = new Map();
                }

                map.TileSetFile = input.ReadString();
                map.Dimensions = input.ReadObject<Point>();
                map.Tiles = input.ReadObject<int[]>();

                return map;
            }
        }
    }
}

What else do I need?

2
The XML file is part of your content project? if so, what is set for its Content Importer and Content Processor?lysergic-acid

2 Answers

3
votes

When your solution builds, your content project is being built first (before the xna game proj which holds the Map class). While building content, it is trying to reference the Map class which hasn't been built yet so it has no knowledge of it.

The map class needs to go in a separate project that builds first (set the xna game proj to depend on it). Add that proj to the references for the xna proj and add some using statements if necessary. This is how all the samples on the app hub educational section do it.

0
votes

This is just a guess but doesn't your Map class need to be public? It's internal in your code.