是否可以在反序列化时跳过无效值?例如,如果用户在xml文件中插入了无效值。
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;
}
}
}
// ...
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 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 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>
我不想“重置”用户设置,我只想跳过无效的内容并使用默认值。否则,我会尝试一个try / catch构造,只需重新生成xml文件。
答案 0 :(得分:2)
不幸的是,当遇到未知的XmlSerializer
值时,无法在enum
内抑制异常。相反,您需要为此目的创建一个string
- 值的属性,并序列化该属性而不是enum
- 值的属性:
[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;
}
}