BrainInBlack
BrainInBlack

Reputation: 139

Skipping Invalid Values On Deserialize

Question

Is it possible to skip invalid values up on de-serialization? For example if a user inserted a invalid value inside the xml file.

Class Definition

using Relink.Data.Enum;
using System;
using System.IO;
using System.Xml.Serialization;
using System.ComponentModel; 

namespace Relink {

    [Serializable]
    public class Settings {

        internal static XmlSerializer Serializer = new XmlSerializer(typeof(Settings));

        public Difficulty Difficulty {
            get;
            set;
        }

        public Boolean CaptureMouse {
            get;
            set;
        }

        internal void loadDefaults() {
            this.Difficulty = Difficulty.Normal;
            this.CaptureMouse = false;
        }

    }

}

Serialization Method

// ...
if(!File.Exists(GameDir + SettingsFile)) {
    Settings = new Settings();
    Settings.loadDefaults();
    TextWriter writer = new StreamWriter(GameDir + SettingsFile);
    Settings.Serializer.Serialize(writer, Settings);
    writer.Close();
    writer.Dispose();
} else {
    TextReader reader = new StreamReader(GameDir + SettingsFile);
    Settings = (Settings)Settings.Serializer.Deserialize(reader);
}
// ...

XML Content (valid)

<?xml version="1.0" encoding="utf-8"?>
<Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Difficulty>Normal</Difficulty>
  <CaptureMouse>false</CaptureMouse>
</Settings>

XML Content (invalid)

<?xml version="1.0" encoding="utf-8"?>
<Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Difficulty>Moo</Difficulty>
  <CaptureMouse>false</CaptureMouse>
</Settings>

Remarks

I don't want to "reset" the users settings, i just want to skip the invalid stuff and use default values instead. Otherwise i would you a try/catch construct and just re-generate the xml file.

Upvotes: 2

Views: 565

Answers (1)

dbc
dbc

Reputation: 116731

Unfortunately there is no way to suppress the exception inside XmlSerializer when an unknown enum value is encountered. Instead, you will need to create a string-valued property for this purpose, and serialize that instead of the enum-valued property:

[Serializable]
public class Settings
{
    internal static XmlSerializer Serializer = new XmlSerializer(typeof(Settings));

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    [XmlElement("Difficulty")]
    public string XmlDifficulty
    {
        get
        {
            return Difficulty.ToString();
        }
        set
        {
            try
            {
                Difficulty = (Difficulty)Enum.Parse(typeof(Difficulty), value);
            }
            catch
            {
                Debug.WriteLine("Invalid difficulty found: " + value);
                Difficulty = Difficulty.Normal;
            }
        }
    }

    [XmlIgnore]
    public Difficulty Difficulty { get; set; }

    public Boolean CaptureMouse { get; set; }

    internal void loadDefaults()
    {
        this.Difficulty = Difficulty.Normal;
        this.CaptureMouse = false;
    }
}

Upvotes: 3

Related Questions