XML序列化配置的替代方法 [英] Alternative to XML Serialization for Configuration

查看:81
本文介绍了XML序列化配置的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们使用一个巨大的配置对象,该对象被序列化到XML或从XML序列化.这在大多数情况下都可以正常工作,但是我们发现,在断电和应用程序崩溃的情况下,该文件可能会处于无法正确反序列化的状态,从而有效地破坏了配置信息.

Currently, we use a giant configuration object that is serialized to/from XML. This has worked fine for the most part, but we are finding that in the case of power loss and application crashes, that the file could be left in a state that renders it unable to deserialize properly, effectively corrupting the configuration information.

我想使用内置的app.config,但它似乎不容易支持自定义类.例如,通过XML序列化,我可以轻松地序列化通用list<ComplexClass>而无需额外的代码.它只是工作.似乎在使用app.config时,您必须提供大量信息和自定义类才能使其正常工作.另外,大多数自定义配置"教程来自2007年左右,就我所知而言可能已经过时.是否有人在.NET 4.0中拥有最新的信息来实现此目的?

I would like to use the built-in app.config, but it doesn't seem to easily support custom classes. For example, with XML serialization, I can easily serialize a generic list<ComplexClass> with no extra code. It just works. It seems that when using app.config, you have to provide a ton of information and custom classes for this to work. Plus, most of the "custom configuration" tutorials are from circa-2007 and may be outdated for all I know. Does anyone have information to the latest way to do this in .NET 4.0?

此外,当应用程序出现问题时,它是9/10倍的问题,这是由于配置不当造成的. App.config喜欢将用户可更改的设置存储在一个非常难以访问的位置,以供不熟悉隐藏目录等内容的用户使用.有没有办法在一个位置存储配置文件,当发生问题时用户可以轻松地通过电子邮件将其发送给我们?

In addition, when a problem occurs in the application, 9/10 times it is because of an improper configuration. App.config likes to store user-changeable settings in a very inaccessible location for users who aren't familiar with hidden directories and such. Is there any way to have a single location to store the config file, which the user can easily email us when a problem occurs?

或者,这一切比我记得2.0天内的事情还容易吗?任何有关如何轻松自定义app.config信息的链接或快速示例都很棒.

Or, is all this easier than I remember it being in early 2.0 days? Any links or quick examples of how to easily do custom app.config information would be great.

作为另一个示例,这是我要序列化为List<Alarm>的对象类型之一的简化​​版本,因为Alarm的数量可以变化或为空.有没有类似的方法可以将类似的内容存储在app.config中?

As a further example, this is a pared-down version of one of the types of objects I want to serialize as List<Alarm>, as the amount of Alarms can vary or be empty. Is there an analogous way to store something like this in app.config?

[Serializable]
public class Alarm
{
    [Serializable]
    public class AlarmSetting
    {
        public enum AlarmVariables { Concentration, RSquared }
        public enum AlarmComparisons { LessThan, GreaterThan }

        [Description("Which entity is being alarmed on.")]
        public AlarmVariables Variable { get; set; }
        [Description("Method of comparing the entity to the setpoint.")]
        public AlarmComparisons Comparator { get; set; }
        [Description("Value at which to alarm.")]
        public Double Setpoint { get; set; }
    }

    public String Name { get; set; }
    public Boolean Enabled { get; set; }
    public String Parameter { get; set; }
    public List<AlarmSetting> AlarmSettings { get; set; }
    public System.Drawing.Color RowColor { get; set; }
}

推荐答案

我建议不要使用任何类型的配置文件,而应使用某些类型的本地数据库,例如

I would suggest moving away from any sort of config file and instead use some type of local database such as sqlite or sql server express which is much more resilient to app crashes.

恕我直言,配置设置不应是 user 设置的默认容器.对我来说,那里有一个配置文件,以确保应用程序在给定的环境中运行.例如,定义连接字符串或轮询速率或类似性质的东西.

IMHO, config settings shouldn't be a default container for user settings. To me a config file is there to make sure the app runs in the given environment. For example, defining connection strings or polling rates or things of that nature.

用户设置,尤其是经常更改的用户设置,需要更好的存储机制,例如本地数据库.当然,除非是客户端/服务器应用程序.在这种情况下,这些设置应在服务器本身进行设置,并且仅在应用程序必须处于断开连接状态时才能在本地保留.

User settings, especially ones that change often, need a better storage mechanism such as a local database. Unless, of course, it's a client/server application. In which case those settings should be up at the server itself and only persisted locally if the app has to work in a disconnected state.

您提供的示例(配置一个似乎是一个或多个警报)是数据库表中某些内容的完美示例.

The example you gave, one of configuring what appears to be one or more alarms is a perfect example of something that belongs in a database table.

这篇关于XML序列化配置的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆