Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从App.config读取此自定义配置?_C#_.net_Custom Configuration - Fatal编程技术网

C# 如何从App.config读取此自定义配置?

C# 如何从App.config读取此自定义配置?,c#,.net,custom-configuration,C#,.net,Custom Configuration,如何从App.config读取此自定义配置 <root name="myRoot" type="rootType"> <element name="myName" type="myType" /> <element name="hisName" type="hisType" /> <element name="yourName" type="yourType" /> </root> 与此相反: <

如何从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>

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


有关GetSection()的详细信息,请参阅,因为这不是标准的配置文件格式,所以您必须以XML文档的形式打开配置文件,然后拉出这些节(例如使用XPath)。使用以下命令打开文档:

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

要使集合元素直接位于父元素(而不是子集合元素)中,需要重新定义
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>

我想你可以用

            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");

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

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