Deserialize inherited classes into the same list in XNA

Posted by M0rgenstern on Game Development See other posts from Game Development or by M0rgenstern
Published on 2011-11-27T22:44:55Z Indexed on 2011/11/28 2:04 UTC
Read the original article Hit count: 337

Filed under:
|
|
|

I am writing a Gui for a game (for what else ...). Therefor I wrote a class GuiElement which has some serializeable fields. From this class I deflect a Class "Button" which has one serializeable field more. Furthermore, I have a Class GuiWindow, which is as well deflected from "GuiElement". An Object of this Class has a Field "HandledElements" of the type "List". To know the layout of the Menues, I use XML-Files, which look like that (for example):

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent xmlns:Generic="System.Collections.Generic">
 <Asset Type="System.Collections.Generic.List[GUI.GuiWindow]">
  <Item>
   <Position>0 0</Position>
   <AlternativeImagePath></AlternativeImagePath>
   <IsActive>true</IsActive>
   <Name>MainMenu</Name>
   <HandledElements>
    <Item>
      <Position>100 100</Position>
      <AlternativeImagePath></AlternativeImagePath>
      <IsActive>true</IsActive>
      <Name>Optionen</Name>
      <Caption>Optionen</Caption>
    </Item>
   </HandledElements>
 </Item>

  <Item>
   <Position>0 0</Position>
   <AlternativeImagePath></AlternativeImagePath>
   <IsActive>false</IsActive>
   <Name>Options</Name>
   <HandledElements>
   </HandledElements>
  </Item>
 </Asset>
</XnaContent>

As you can see, the first window has in its "HandledElements" List an Item with the Field . This is a field which only a Button has. The Problem is now: I can't deserialize this XML file, because GuiElement does not have this Field, it only has the few fields above. I thought it would know automatically which Class to use,but it doesn't. Do I really have to give my windows a list for each child class of GuiElement? Or can I do another workaround?

© Game Development or respective owner

Related posts about XNA

Related posts about c#