在反序列化时跳过无效值

编程入门 行业动态 更新时间:2024-10-28 13:25:29
本文介绍了在反序列化时跳过无效值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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内容(有效)

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

XML内容(无效)

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

备注

我不想重置"用户设置,我只想跳过无效的内容,而是使用默认值.否则,我会尝试/捕获构造并重新生成xml文件.

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.

推荐答案

不幸的是,当遇到未知的 enum 值时,无法抑制 XmlSerializer 内部的异常.取而代之的是,您需要为此创建一个 string 值的属性,并对其进行序列化,而不是 enum 值的属性:

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; } }

更多推荐

在反序列化时跳过无效值

本文发布于:2023-08-04 05:51:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳过   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!