有没有办法在应用程序设置中使用字典或 xml?
我必须在应用程序设置中存储复杂类型。我认为将其存储为 XML 效果最好。
问题是我不知道如何存储 XML。我更喜欢将其存储为托管 XML,而不是仅使用必须在每次访问时解析它的原始 XML 字符串。我设法将设置的 Type
列设置为 XDocument,但无法设置其值。
有没有办法在应用程序设置中使用 XDocument 或 XML?
更新
我找到了一种方法,只需使用 xml 编辑器编辑 .settings 文件即可。
我将其更改为 自定义可序列化字典,但当我尝试访问该设置时出现以下错误-property(我将其设置为默认值的序列化表示)。
The property 'Setting' could not be created from it's default value.
Error message: There is an error in XML document (1, 41).
任何想法将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我所做的(即使我不太喜欢 - 但有效)是:
我创建了我的值的简单可序列化类:
然后在 .settings 文件中,我编辑了设置的类型(使用 xml 打开的 .setting 文件)编辑器)到房间的全名(即MyProject.Rooms)。
然后,我为自己制作了一个反序列化示例,并将其复制到 .settings 编辑器中的值字段以获得默认值。效果很好。
它确实不是字典,但我仍然可以在 Rooms 类和内部字典中实现(按 Room.Room 返回部分。
任何好的想法仍然受到欢迎,我仍在寻找一种能够使用
的方法.settings 文件中的 IDictinoary
此外,我打开了一个 连接 ,请善意地投票!
What I did (even I don't like so much - but works) is:
I created simple serializable classes of my values:
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
IDictinoary
s in the .settings file.Besides, I opened a connection for it, please be kind and vote!
我最终所做的是使用 .NET Fx 中的内置配置类,因为这是阻力最小的路径。即使下面的代码是用 C# 编写的,您也应该能够轻松地将其转换为 VB.NET(或者将其编辑并编译为可以从项目中引用的程序集)。
您会注意到,ConfigurationElementCollection 类可以毫无困难地转换为键/值对的字典(您可能必须对值对使用反射,或者您想要存储为值对的类可以采用从 ConfigurationElement 继承的设置类作为构造函数参数)。
下面是一个示例,我在系统中使用上面的基类代码作为我们的分片策略(更改名称是为了保护无辜者):
然后是上面的 XML 实例表示的配置类:
虽然 *.config 中的 IDictionary 不是真正的 IDictionary文件它可以处理该作业,如果您的配置文件在运行时更新,您不必重新启动应用程序或回收 AppPool 来获取新值。
What I ended up doing because this was the path of least resistance was use the built-in Configuration classes in the .NET Fx. Even though the below code is in C# you should be able to convert it to VB.NET with little difficulty (or edit and compile it into an assembly you can reference from your project).
You will note that the ConfigurationElementCollection class can be converted into a dictionary of key/value pairs with little difficulty (you may have to use reflection for the value pairs or the classes you want to store as value pairs could take settings classes that inherit from ConfigurationElement as a constructor argument).
And here's an example where I use the above base class code for our sharding strategy in our system (names altered to protect the innocent):
And then the configuration classes represented by the XML instance above:
While its not true IDictionary in a *.config file it can handle the job, and if your configuration file is updated at runtime you don't have to restart the application or recycle the AppPool to get the new values.