C# 带有键和属性的自定义配置节
我需要创建一个类似appsettings的配置部分,但在周围的元素上有一个属性,例如:C# 带有键和属性的自定义配置节,c#,configuration,web-config,C#,Configuration,Web Config,我需要创建一个类似appsettings的配置部分,但在周围的元素上有一个属性,例如: <userSettings> <machine name="my-machine"> <setting key="Site" value="http://localhost" /> <setting key="Online" value="true" /> </machine> <!--
<userSettings>
<machine name="my-machine">
<setting key="Site" value="http://localhost" />
<setting key="Online" value="true" />
</machine>
<!-- more machines -->
</userSettings>
和背景
public class SettingElement : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true, IsKey = true)]
public string Key
{
get { return this["key"].ToString(); }
}
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return this["value"].ToString(); }
}
}
为什么不创建一个新的xml文件,而不是使用配置文件呢。如果您使用xml文件,管理和操作将非常容易,但实际上没有帮助……为什么不创建一个新的xml文件,而不是使用配置文件呢。如果您使用xml文件,它将非常容易管理和操作,这对您毫无帮助。。。
public class SettingElement : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true, IsKey = true)]
public string Key
{
get { return this["key"].ToString(); }
}
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return this["value"].ToString(); }
}
}