For example if I have two settings classes, CameraSettings and EnemySettings how can I determine which class I am dealing with when serializing/deserializing their properties using just one XML file?
At the moment when dealing with just one class and I am doing the following for saving:
private void XMLSave(object sender, System.EventArgs e) { object currentSettings = Game1.Instance.camera.Settings; if (currentSettings == null) { return; } XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; using (XmlWriter xmlWriter = XmlWriter.Create("AllSettings.xml", settings)) { IntermediateSerializer.Serialize(xmlWriter, currentSettings, null); } }
and the following for loading:
private void XMLLoad(object sender, System.EventArgs e) { Stream stream = null; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.CheckFileExists = true; openFileDialog.CheckPathExists = true; openFileDialog.InitialDirectory = "\\"; openFileDialog.Filter = "xml files (*.xml)|*.xml"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { if ((stream = openFileDialog.OpenFile()) != null) { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; using (XmlReader reader = XmlReader.Create(stream, settings)) { // - Clear the PhysX scene so no actors and entities are alive // - Create new entities/actors from settings Game1.Instance.camera.Settings = IntermediateSerializer.Deserialize<CameraSettings>(reader, null); } } } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } }