【问题标题】:How to read this custom configuration from App.config?如何从 App.config 中读取此自定义配置?
【发布时间】:2011-06-15 10:26:07
【问题描述】:

如何从 App.config 中读取这个自定义配置?

<root name="myRoot" type="rootType">
    <element name="myName" type="myType" />
    <element name="hisName" type="hisType" />
    <element name="yourName" type="yourType" />
  </root>

而不是这样:

<root name="myRoot" type="rootType">
  <elements>
    <element name="myName" type="myType" />
    <element name="hisName" type="hisType" />
    <element name="yourName" type="yourType" />
  </elements>
  </root>

【问题讨论】:

  • 如果这些答案不能完全帮助您,请提供更多信息,以便我们进一步提供帮助。

标签: c# .net custom-configuration


【解决方案1】:

要使您的集合元素直接位于父元素(而不是子集合元素)中,您需要重新定义您的ConfigurationProperty。例如,假设我有一个集合元素,例如:

public class TestConfigurationElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
    }
}

还有一个集合如:

[ConfigurationCollection(typeof(TestConfigurationElement), AddItemName = "test")]
public class TestConfigurationElementCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new TestConfigurationElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((TestConfigurationElement)element).Name;
    }
}

我需要将父节/元素定义为:

public class TestConfigurationSection : ConfigurationSection
{
    [ConfigurationProperty("", IsDefaultCollection = true)]
    public TestConfigurationElementCollection Tests
    {
        get { return (TestConfigurationElementCollection)this[""]; }
    }
}

注意[ConfigurationProperty("", IsDefaultCollection = true)] 属性。给它一个空名称,并将其设置为默认集合允许我定义我的配置,如:

<testConfig>
  <test name="One" />
  <test name="Two" />
</testConfig>

代替:

<testConfig>
  <tests>
    <test name="One" />
    <test name="Two" />
  </tests>
</testConfig>

【讨论】:

    【解决方案2】:

    您可以使用 System.Configuration.GetSection() 方法来读取自定义配置部分。

    请参阅http://msdn.microsoft.com/en-us/library/system.configuration.configuration.getsection.aspx 了解有关 GetSection() 的更多信息

    【讨论】:

      【解决方案3】:

      由于这不是标准配置文件格式,您必须将配置文件作为 XML 文档打开,然后提取部分(例如使用 XPath)。用这个打开文档:

      // Load the app.config file
      XmlDocument xml = new XmlDocument();
      xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
      

      【讨论】:

        【解决方案4】:

        我认为你可以使用

                    XmlDocument appSettingsDoc = new XmlDocument();
                    appSettingsDoc.Load(Assembly.GetExecutingAssembly().Location + ".config");
                    XmlNode node = appSettingsDoc.SelectSingleNode("//appSettings");
        
                    XmlElement element= (XmlElement)node.SelectSingleNode(string.Format("//add[@name='{0}']", "myname"));
                    string typeValue = element.GetAttribute("type");
        

        希望这能解决您的问题。快乐编码。 :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-12
          • 2012-01-28
          • 2015-12-30
          • 2010-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-02
          相关资源
          最近更新 更多