有没有办法在应用程序设置中使用字典或xml? [英] Is there a way to use a dictionary or xml in the Application Settings?

查看:254
本文介绍了有没有办法在应用程序设置中使用字典或xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在应用程序设置中存储一个复杂的类型。我认为将其存储为XML将会工作得最好。



问题是我不知道如何存储XML。我更喜欢将其存储为托管XML,而不是仅使用原始XML字符串在每个访问上解析它。我设法将设置的类型列设置为XDocument,但是我无法设置其值。



有没有办法在应用程序设置中使用XDocument或XML?



更新



我找到了一种方法,只需使用xml编辑器编辑.settings文件。



我将其更改为自定义可序列化字典,但是当我尝试访问setting-property(我将其设置为默认值的序列化表示)时,我收到以下错误。 / p>

 属性设置无法从其默认值创建。 
错误消息:XML文档中有错误(1,41)。

任何想法将不胜感激。

解决方案

我做了什么(即使我不喜欢这么多 - 但是工作)是:



我创建了简单的可序列化的我的类值:

 < Xml.Serialization.XmlRoot(Rooms)> _ 
公共课室:继承人($)
结束类

< Serializable()> _
公共课室
私人m_Room As String
公共房间()As String
获取
返回m_Room
结束获取
设置( ByVal值As String)
m_Room = value
结束集
结束属性

私有m_Sections作为新列表(部分)
公共ReadOnly属性部分)作为列表(部分)
获取
返回m_Sections
结束获取
结束属性
结束类

< Serializable()> ; _
公共类节
私有m_Section As String
公共属性Section()As String
获取
返回m_Section
结束Get
Set( ByVal值As String)
m_Section = value
结束集
结束属性
结束类

然后在.settings文件中,我将设置的类型(使用xml编辑器打开的.setting文件)编辑为房间的全名(即MyProject.Rooms)。
然后我做了一个样本反序列化示例,并将其复制到.settings编辑器中的值字段以具有默认值。它的效果很好
确实不是字典,但是我仍然可以在Rooms类和内部字典中实现(通过Room.Room返回Sections。



任何好的想法仍然很受欢迎,我仍然在寻找一种方法来在.settings文件中使用 IDictinoary



<另外,我打开了一个连接,请妥善和投票!


I have to store a complex type in the application settings. I thought that storing it as XML would work best.

The problem is I don't know how store XML. I prefer to store it as a managed XML rather than using just a string of raw XML having to parse it on each access. I managed to set the Type column of the setting to XDocument, but I was unable to set its value.

Is there a way to use XDocument or XML in application settings?

Update

I found a way, simply by editing the .settings file with the xml editor.

I changed it to a custom serializable dictionary, but I get the following error when I try to access the setting-property (I set it to a serialized representation of the default value).

The property 'Setting' could not be created from it's default value.
Error message: There is an error in XML document (1, 41).

Any ideas will be appreciated.

解决方案

What I did (even I don't like so much - but works) is:

I created simple serializable classes of my values:

<Xml.Serialization.XmlRoot("Rooms")> _
Public Class Rooms : Inherits List(Of Room)
End Class

<Serializable()> _
Public Class Room
    Private m_Room As String
    Public Property Room() As String
        Get
            Return m_Room
        End Get
        Set(ByVal value As String)
            m_Room = value
        End Set
    End Property

    Private m_Sections As New List(Of Section)
    Public ReadOnly Property Sections() As List(Of Section)
        Get
            Return m_Sections
        End Get
    End Property
End Class

<Serializable()> _
Public Class Section
    Private m_Section As String
    Public Property Section() As String
        Get
            Return m_Section
        End Get
        Set(ByVal value As String)
            m_Section = value
        End Set
    End Property
End Class

Then in the .settings file, I edited the type of the setting (the .setting file opened with the xml editor) to full name of Rooms (i.e. MyProject.Rooms). Then I made myself a sample deserialized sample and copied it to the value field in the .settings editor to have a default value. it works great. It is indeed not a dictionary, but I could still implement in the Rooms class and internal dictionary (that returns Sections by Room.Room.

Any good ideas are still welcommed, I am still looking for a way to be able to use IDictinoarys in the .settings file.

Besides, I opened a connection for it, please be kind and vote!

这篇关于有没有办法在应用程序设置中使用字典或xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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