Original Post
Reading through Shawn Hargreaves blog on XML he shows a useful method for dealing with polymorphic classes and XML:
The problem I having trouble understanding is how can the correct inherited classes be loaded from such an XML file?
At the moment I am loading using the following:
If the SaveData would contain an array of the base class, how would I make sure the inherited classes were loaded correctly after deserializing the XML file and creating the original array?
Quote:
One advantage of IntermediateSerializer over XmlSerializer is that it supports arbitrary polymorphic data without requiring all the possible subtypes to be predeclared. Let's look at an example:
class A { public bool Value = true; } class B : A { } class C : A { } class TestClass { public object Hello = "World"; public object Elf = 23; public A[] PolymorphicArray = { new A(), new B(), new C() }; }Resulting XML: <?xml version="1.0" encoding="utf-8"?> <XnaContent> <Asset Type="TestClass"> <Hello Type="string">World</Hello> <Elf Type="int">23</Elf> <PolymorphicArray> <Item> <Value>true</Value> </Item> <Item Type="B"> <Value>true</Value> </Item> <Item Type="C"> <Value>true</Value> </Item> </PolymorphicArray> </Asset> </XnaContent>The problem I having trouble understanding is how can the correct inherited classes be loaded from such an XML file?
At the moment I am loading using the following:
if ((stream = openFileDialog.OpenFile()) != null) { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; using (XmlReader reader = XmlReader.Create(stream, settings)) { // - Create new entities/actors from settings SaveData saveData = IntermediateSerializer.Deserialize<SaveData>(reader, null); // Load Camera Game1.Instance.camera = new Camera_FreeLook(saveData.cameraSettings); }If the SaveData would contain an array of the base class, how would I make sure the inherited classes were loaded correctly after deserializing the XML file and creating the original array?