SortedList's Key with content pipeline

Last post 04-29-2008, 12:46 PM by Shawn Hargreaves. 1 replies.
Sort Posts: Previous Next
  •  04-28-2008, 8:40 PM

    SortedList's Key with content pipeline

    I'm looking to load a SortedList via xml and the content pipeline, but I'm getting an error that I can't seem to resolve.  With "Controls" being my sorted list, here's what my XML looks like:

     
        <Controls>
          <Capacity>4</Capacity>
          <Item>
            <Value Type="Menu.Label">
              <Name>ExitGame</Name>
              <Position>55 105</Position>
              <Color>FFFFFFFF</Color>
              <FontName>Fonts/Kootenay20</FontName>
              <Text>Exit Game</Text>
            </Value>
          </Item>
        </Controls>

    Which gives me the error:

    Error 1 There was an error while deserializing intermediate XML. Value cannot be null.
    Parameter name: key C:\Programming\DCF\MenuContent\Content\Windows\MainMenu\Layout.xml 14 11 MenuContent

    I've tried adding key as an element and/or an attribute of Item and Value, but to no avail.  I get the same error every time.  Interestingly, the a test output from the IntermediateSerializer gives:

        <Controls>
    <Capacity>4</Capacity>
    <Item>
    <Value>
    <Name></Name>
    <Position>0 0</Position>
    </Value>
    </Item>
    </Controls>
    Which also has no mention of the key...  any help would be great appreciated.  Thanks!
  •  04-29-2008, 12:46 PM

    Re: SortedList's Key with content pipeline

    I don't think the automatic serialization will be able to handle the SortedList type.

    The XML serializer normally works by creating an instance of the class, then using reflection to set all its public fields and properties. It also has some special logic to understand types that implement the ICollection interface, and use the Add method to read the collection items.

    This doesn't work for dictionary types, though (and SortedList is just a specific implementation of IDictionary). Dictionaries are a collection of KeyValuePair<K,V>, but KeyValuePair doesn't have any parameterless constructor, so it is not possible for the serializer to construct instances of this type.

    For this reason, the standard .NET XmlSerializer doesn't support dictionaries at all. Our content pipeline serializer
    does one better than that and knows how to automatically serialize the standard Dictionary<K,V> class, but it still doesn't understand other dictionary implementations such as SortedList.

    The easiest thing here would be to use a regular dictionary instead of SortedList.

    If you absolutely need to make this work, you can implement a custom ContentTypeSerializer which will manually tell the pipeline how to read and write this type to XML (ContentTypeSerializer is how you customize the behavior for types that can't be handled properly by the default reflection based serialization). To learn how to do this, use Reflector to look at our built in DictionarySerializer implementation.

    --
    XNA Framework Developer
    blog - homepage
View as RSS news feed in XML
©2007 Microsoft Corporation. All rights reserved. Privacy Statement Terms of Use Code of Conduct Feedback