XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Blank Serialized File

Last post 07/11/2009 18:20 by Shawn Hargreaves. 5 replies.
  • 06/11/2009 15:25

    Blank Serialized File

    I've been trying out the automatic content serialization that Shawn describes on his blog and I've come across a bit of a problem. So far I've created a library called GameData, which has a data class in it to be serialized, and I've added that library to my game and the game's content project. I've then created an instance of the data class and filled it with data, before serializing it to an xml file using the intermediate serializer (as described here). I then copied this serialized file to the game content project, and tried to load it in the LoadContent section of my game. This all compiles correctly, and the xnb file appears, but when loaded the file contains no data. If I deserialize the xml file manually then the content loads perfectly.

    I'm probably missing something obvious, but any help would be most welcome. :)
    Current Projects: Rule the World
  • 06/11/2009 16:31 In reply to

    Re: Blank Serialized File

    Can we see the type in question?
    XNA Framework Developer - blog - homepage
  • 06/11/2009 17:01 In reply to

    Re: Blank Serialized File

    Sure, here are the data types that I'm trying to save/load:

    public class PlayerData 
        { 
            public string Name { getset; } 
            public Color Colour { getset; } 
        } 
        public class CountryData 
        { 
            public string Name { getset; } 
            public Vector3 Position { getset; } 
        } 
        public class ContinentData 
        { 
            public string Name { getset; } 
            public int Value { getset; } 
            public Collection<CountryData> Countries { getset; } 
            public ContinentData() 
            { 
                this.Countries = new Collection<CountryData>(); 
            } 
        } 
        public class CountryLinkData 
        { 
            public string One { getset; } 
            public string Two { getset; } 
            public CountryLinkData() 
            { 
            } 
        } 
        public class GameData 
        { 
            public Collection<PlayerData> Players { getset; } 
            public Collection<ContinentData> Continents { getset; } 
            public Collection<CountryLinkData> Links { getset; } 
            public GameData() 
            { 
                this.Players = new Collection<PlayerData>(); 
                this.Continents = new Collection<ContinentData>(); 
                this.Links = new Collection<CountryLinkData>(); 
            } 
        } 

    And here's an example of the serialized xml:

    <?xml version="1.0" encoding="utf-8"?> 
    <XnaContent> 
      <Asset Type="RiskData.GameData"
        <Players /> 
        <Continents> 
          <Item> 
            <Name>North America</Name> 
            <Value>0</Value> 
            <Countries> 
              <Item> 
                <Name>Alaska</Name> 
                <Position>615.5 0 640.5</Position> 
              </Item> 
              <Item> 
                <Name>Western Canada</Name> 
                <Position>1301.5 0 917</Position> 
              </Item> 
              <Item> 
                <Name>Central America</Name> 
                <Position>1538 0 2170</Position> 
              </Item> 
              <Item> 
                <Name>Eastern United States</Name> 
                <Position>1785.5 0 1539</Position> 
              </Item> 
              <Item> 
                <Name>Greenland</Name> 
                <Position>3713 0 376</Position> 
              </Item> 
              <Item> 
                <Name>Northwest Territory</Name> 
                <Position>1687 0 500</Position> 
              </Item> 
              <Item> 
                <Name>Central Canada</Name> 
                <Position>1602 0 1038</Position> 
              </Item> 
              <Item> 
                <Name>Eastern Canada</Name> 
                <Position>2572 0 973.5</Position> 
              </Item> 
              <Item> 
                <Name>Western United States</Name> 
                <Position>1143 0 1435</Position> 
              </Item> 
            </Countries> 
          </Item> 
          <Item> 
            <Name>South America</Name> 
            <Value>0</Value> 
            <Countries> 
              <Item> 
                <Name>Argentina</Name> 
                <Position>2502 0 4249.5</Position> 
              </Item> 
              <Item> 
                <Name>Brazil</Name> 
                <Position>2687.5 0 3421</Position> 
              </Item> 
              <Item> 
                <Name>Peru</Name> 
                <Position>2258 0 3390.5</Position> 
              </Item> 
              <Item> 
                <Name>Venezuela</Name> 
                <Position>2308 0 2756.5</Position> 
              </Item> 
            </Countries> 
          </Item> 
          <Item> 
            <Name>Europe</Name> 
            <Value>0</Value> 
            <Countries> 
              <Item> 
                <Name>Great Britain</Name> 
                <Position>4457 0 931.5</Position> 
              </Item> 
              <Item> 
                <Name>Iceland</Name> 
                <Position>4132 0 570.5</Position> 
              </Item> 
              <Item> 
                <Name>Northern Europe</Name> 
                <Position>4966.5 0 1020</Position> 
              </Item> 
              <Item> 
                <Name>Scandinavia</Name> 
                <Position>5070.5 0 628.5</Position> 
              </Item> 
              <Item> 
                <Name>Southern Europe</Name> 
                <Position>5088.5 0 1328.5</Position> 
              </Item> 
              <Item> 
                <Name>Russia</Name> 
                <Position>5765 0 943</Position> 
              </Item> 
              <Item> 
                <Name>Western Europe</Name> 
                <Position>4525 0 1304</Position> 
              </Item> 
            </Countries> 
          </Item> 
          <Item> 
            <Name>Africa</Name> 
            <Value>0</Value> 
            <Countries> 
              <Item> 
                <Name>Congo</Name> 
                <Position>5274.5 0 2948</Position> 
              </Item> 
              <Item> 
                <Name>East Africa</Name> 
                <Position>5762.5 0 2786.5</Position> 
              </Item> 
              <Item> 
                <Name>Egypt</Name> 
                <Position>5274.5 0 1937</Position> 
              </Item> 
              <Item> 
                <Name>Madagascar</Name> 
                <Position>6088.5 0 3614</Position> 
              </Item> 
              <Item> 
                <Name>North Africa</Name> 
                <Position>4636 0 2181.5</Position> 
              </Item> 
              <Item> 
                <Name>South Africa</Name> 
                <Position>5412 0 3655</Position> 
              </Item> 
            </Countries> 
          </Item> 
          <Item> 
            <Name>Asia</Name> 
            <Value>0</Value> 
            <Countries> 
              <Item> 
                <Name>Afghanistan</Name> 
                <Position>6506 0 1248</Position> 
              </Item> 
              <Item> 
                <Name>China</Name> 
                <Position>7708 0 1668</Position> 
              </Item> 
              <Item> 
                <Name>India</Name> 
                <Position>7103.5 0 2089</Position> 
              </Item> 
              <Item> 
                <Name>Irkutsk</Name> 
                <Position>7804 0 855.5</Position> 
              </Item> 
              <Item> 
                <Name>Japan</Name> 
                <Position>8875 0 1505.5</Position> 
              </Item> 
              <Item> 
                <Name>Kamchatka</Name> 
                <Position>8825 0 878</Position> 
              </Item> 
              <Item> 
                <Name>Middle East</Name> 
                <Position>5982 0 1900.5</Position> 
              </Item> 
              <Item> 
                <Name>Mongolia</Name> 
                <Position>7920.5 0 1287.5</Position> 
              </Item> 
              <Item> 
                <Name>Southeast Asia</Name> 
                <Position>7903.5 0 2370</Position> 
              </Item> 
              <Item> 
                <Name>Siberia</Name> 
                <Position>6990.5 0 659</Position> 
              </Item> 
              <Item> 
                <Name>Ural</Name> 
                <Position>6619.5 0 767.5</Position> 
              </Item> 
              <Item> 
                <Name>Yakutsk</Name> 
                <Position>7918.5 0 581</Position> 
              </Item> 
            </Countries> 
          </Item> 
          <Item> 
            <Name>Oceania</Name> 
            <Value>0</Value> 
            <Countries> 
              <Item> 
                <Name>Eastern Australia</Name> 
                <Position>9159.5 0 3921</Position> 
              </Item> 
              <Item> 
                <Name>Indonesia</Name> 
                <Position>8291 0 2759</Position> 
              </Item> 
              <Item> 
                <Name>New Guinea</Name> 
                <Position>9404 0 3069</Position> 
              </Item> 
              <Item> 
                <Name>Western Australia</Name> 
                <Position>8698 0 3865.5</Position> 
              </Item> 
            </Countries> 
          </Item> 
        </Continents> 
        <Links /> 
      </Asset> 
    </XnaContent> 

    Current Projects: Rule the World
  • 06/11/2009 17:34 In reply to

    Re: Blank Serialized File

    Answer
    Reply Quote
    The problem is that there is no built-in ContentTypeWriter/Reader for System.Collections.ObjectModel.Collection<T>.

    You can either write such a ContentTypeWriter/Reader pair, or (probably easier) just change your Collection members to a type that is directly supported (I would recommend either List<T> or perhaps even a T[] array).

    XNA Framework Developer - blog - homepage
  • 07/11/2009 17:12 In reply to

    Re: Blank Serialized File

    Thanks for that, it makes complete sense. Is there a list of built-in ContentTypeWriters for XNA somewhere? And is there any way of knowing when xna hasn't found a writer for an object, or does it always just fail silently?
    Current Projects: Rule the World
  • 07/11/2009 18:20 In reply to

    Re: Blank Serialized File

    Steve Johnstone:
    Is there a list of built-in ContentTypeWriters for XNA somewhere?


    Not that I know of.  There probably should be though  :-)

    Steve Johnstone:
    And is there any way of knowing when xna hasn't found a writer for an object, or does it always just fail silently?


    Unfortunately not, as of Game Studio 3.1.  This was a negative side effect of adding the automatic XNB serialization feature.  Prior to that, trying to serialize a type that had no writer was always an error. But now, if a type has no built-in writer, the automatic serialization feature tries to create one for it.  Trouble is, that doesn't work for complex types such as Collection<T>, which have data that cannot be serialized just by grabbing the values of all public fields and properties. But the automatic serializer has no way to distinguish such complex types from simple types that it can handle (that requires knowledge of the semantics of the type, and isn't something you can determine automatically using reflection), so it has a go at serializing the type, and just doesn't save out all the neccessary data.
    XNA Framework Developer - blog - homepage
Page 1 of 1 (6 items) Previous Next