Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跳过反序列化时的无效值 问题:_C#_.net_Xml - Fatal编程技术网

C# 跳过反序列化时的无效值 问题:

C# 跳过反序列化时的无效值 问题:,c#,.net,xml,C#,.net,Xml,是否可以在反序列化时跳过无效值?例如,如果用户在xml文件中插入了无效值 类定义 序列化方法 XML内容(有效) 正常的 假的 XML内容(无效) 哞 假的 评论 我不想“重置”用户设置,我只想跳过无效的内容并使用默认值。否则,我将使用try/catch构造并重新生成xml文件。不幸的是,当遇到未知的enum值时,无法抑制XmlSerializer中的异常。相反,您需要为此创建一个string-值属性,并序列化该属性而不是enum-值属性: [Serializable] public c

是否可以在反序列化时跳过无效值?例如,如果用户在xml文件中插入了无效值

类定义 序列化方法 XML内容(有效)

正常的
假的
XML内容(无效)

哞
假的
评论
我不想“重置”用户设置,我只想跳过无效的内容并使用默认值。否则,我将使用try/catch构造并重新生成xml文件。

不幸的是,当遇到未知的
enum
值时,无法抑制
XmlSerializer
中的异常。相反,您需要为此创建一个
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;
    }
}

嗯,这仍然是一个比我想象的更好的解决方案。谢谢;)您仍然可以使用XDocument或XmlDocument手动编写自定义序列化/反序列化方法。简单的单元测试可以确保往返(序列化和反序列化)不会导致数据丢失。只需比较所有公共属性。
// ...
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>
[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;
    }
}