.NET 中的自定义配置
我看到有关 .NET 中自定义配置的简单示例。 我的情况有点复杂,有嵌套节点。
我希望能够从配置文件中读取此内容:
<environments>
<environment name="live" url="http://www.live.com">
<server name="a" IP="192.168.1.10"></server>
<server name="b" IP="192.168.1.20"></server>
<server name="c" IP="192.168.1.30"></server>
</environment>
<environment name="dev" url="http://www.dev.com">
<server name="a" IP="192.168.1.10"></server>
<server name="c" IP="192.168.1.30"></server>
</environment>
<environment name="test" url="http://www.test.com">
<server name="b" IP="192.168.1.20"></server>
<server name="d" IP="192.168.1.40"></server>
</environment></environments>
如果有人可以为此提供一些代码,我将不胜感激。
谢谢!
I am seeing simple examples regarding custom configuration in .NET. My case is a bit more complex, with nested nodes.
I'd like to be able to read this from the configuration file:
<environments>
<environment name="live" url="http://www.live.com">
<server name="a" IP="192.168.1.10"></server>
<server name="b" IP="192.168.1.20"></server>
<server name="c" IP="192.168.1.30"></server>
</environment>
<environment name="dev" url="http://www.dev.com">
<server name="a" IP="192.168.1.10"></server>
<server name="c" IP="192.168.1.30"></server>
</environment>
<environment name="test" url="http://www.test.com">
<server name="b" IP="192.168.1.20"></server>
<server name="d" IP="192.168.1.40"></server>
</environment></environments>
If anyone could provide some code for that, I'd appreciate it.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过实现从 ConfigurationElement 类继承的自定义配置类来阅读此内容。
以下是“server”元素的示例:
环境元素实际上是一个集合,可以这样实现:
You can read this by implementing custom configuration classes inheriting from the ConfigurationElement class.
Here is an example of the "server" element:
The environment element is actually a collection and could be implemented like this:
我最近遇到的一个很棒的工具是 CodePlex 上的配置部分设计器; 很好地集成到 VS 中。
生成类、XML 架构等。推荐。
A nifty tool I came across recently is the Configuration Section Designer up on CodePlex; integrates into VS nicely.
Generates classes, XML schema, etc. Recommended.
您需要创建一些类来处理配置文件的自定义配置部分。
Phil Haack 对此有一篇非常好的博客文章:通过 3 个简单步骤实现自定义配置部分。
编辑:
我试图找到我过去使用过的代码项目文章来学习如何实现这一目标,我在 Phil 上找到了它博客条目:
这里是揭开.NET 2.0 配置的神秘面纱,
它包含处理嵌套元素和集合所需的信息。
Your need to create some classes to handle the custom configuration sections of the config file.
There's a very good blog entry by Phil Haack on this: Custom Configuration Sections in 3 Easy Steps.
Edit:
I was trying to find the Code Project article I've used in the past to learn how to achieve this, and I've found it on Phil's blog entry:
here it is Unraveling the Mysteries of .NET 2.0 Configuration
it contains the info necessary to handle nested elements and collections.
此示例可能会有所帮助:创建自定义配置节处理程序 (MSDN)
This example might help : To create a custom configuration section handler (MSDN)
查看示例的内容(针对不同的暂存环境),您可能需要查看 Web 部署项目,如 回答之前的问题。
相反,它们在部署时处理配置合并:一个好主意,特别是如果您有敏感信息,例如带有服务帐户密码的连接字符串......
Looking at the content of your example (targeting different staging environments), rather than reading config files dynamically, you might want to look at web deployment projects, as indicated in the answer to an earlier question.
They handle config merging at deployment time instead: a good idea, especially if you have sensitive information like connection strings with service account passwords...
我会使用 App.Config 文件
System.Configuration.ConfigurationSettings.AppSettings["DatabasePath"];
http://www.eggheadcafe.com/community/ aspnet/2/10004734/appconfig-file-c.aspx
I would use App.Config file
System.Configuration.ConfigurationSettings.AppSettings["DatabasePath"];
http://www.eggheadcafe.com/community/aspnet/2/10004734/appconfig-file-c.aspx
以下是我在阅读本文后最近实现的:
使用反射将类绑定到 .ini 文件
http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!174.entry
Here is what I implemented recently after reading this article:
Using Reflection for binding classes to .ini files
http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!174.entry