Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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# 使用自定义xml标记设置App.Config_C#_.net_App Config - Fatal编程技术网

C# 使用自定义xml标记设置App.Config

C# 使用自定义xml标记设置App.Config,c#,.net,app-config,C#,.net,App Config,目前,在正确设置时存在一些问题。我在web上对此做了大量阅读,并尝试了使用不同代码的多种配置,但对于以下自定义xml标记,我似乎无法使其正常工作。有人能就如何解决这个问题提供一些反馈吗?我想从其他类外部访问它,并遍历每个“trackInfo”标记以查找某个键。最后一段代码是我试图提取元素并将其保存为变量的地方 我正在寻找的东西: -我错过了什么? -我如何在课外学习 App.config: <?xml version="1.0" encoding="utf-8" ?> <con

目前,在正确设置时存在一些问题。我在web上对此做了大量阅读,并尝试了使用不同代码的多种配置,但对于以下自定义xml标记,我似乎无法使其正常工作。有人能就如何解决这个问题提供一些反馈吗?我想从其他类外部访问它,并遍历每个“trackInfo”标记以查找某个键。最后一段代码是我试图提取元素并将其保存为变量的地方

我正在寻找的东西: -我错过了什么? -我如何在课外学习

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="trackLog" type="Tracking.TrackConfigSection,   Tracking" />
  </configSections>

  <trackLog>
    <trackInfo key="DEV1" env="dev1" url="http://dev1.com" />
    <trackInfo key="DEV2" env="dev2" url="http://dev2.com" />
  </trackLog>
</configuration>
TrackConfigCollection.cs

[ConfigurationCollection(typeof(TrackConfigEntry), CollectionType = ConfigurationElementCollectionType.BasicMap)] 
        public class TrackConfigCollection : ConfigurationElementCollection
        {
            const string ItemName = "trackInfo";

            public override ConfigurationElementCollectionType CollectionType
            {
                get 
                { 
                    return ConfigurationElementCollectionType.BasicMap; 
                }
            }

            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((TrackConfigEntry)element).Key;
            }

            protected override ConfigurationElement CreateNewElement()
            {
                return new TrackConfigEntry();
            }

            public new TrackConfigEntry this[string key]
            {
                get 
                { 
                    return (TrackConfigEntry)BaseGet(key); 
                }
            }

            public bool Contains(string role)
            {
                return BaseGetAllKeys().Contains(role);
            }

        }
TrackConfigEntry.cs

public class TrackConfigSection : ConfigurationSection
{
    private const string SectionName = "trackLog";

    public TrackConfigSection()
    {
        base[""] = new TrackConfigCollection();
    }

    [ConfigurationProperty("", IsRequired = true, IsKey = false, IsDefaultCollection = true)]
    public TrackConfigCollection TrackConfigs
    {
                get { return ((TrackConfigCollection)(base[""])); }
                set { base[""] = value; }
    }

    public static TrackConfigCollection GetTrackConfigs()
    {
        return ((TrackConfigSection)ConfigurationManager.GetSection(SectionName)).TrackConfigs;
    }
}
public class TrackConfigEntry : ConfigurationElement
{
    [ConfigurationProperty("key", IsRequired = true)]
    public string Key 
    { 
        get { return (string)base["key"]; } 
    }

    [ConfigurationProperty("env", IsRequired = true)]
    public string env 
    { 
        get { return (string)base["env"]; } 
    }

    [ConfigurationProperty("url", IsRequired = true)]
    public string url 
    { 
        get { return (string)base["url"]; } 
    }
}
var envName = System.Configuration.ConfigurationManager.AppSettings["env"];
// the envName is in the tags in the app.config
if (TrackConfigSection.GetTrackConfigs().Contains(envStringName))
{
string temp1 = TrackConfigSection.GetTrackConfig("key").ToString(); // variable to get key
                    string temp2 = TrackConfigSection.GetTrackConfig("env").ToString(); // variable to get env
                    string temp3 = TrackConfigSection.GetTrackConfig("url").ToString(); // variable to get url
}
从其他类访问

public class TrackConfigSection : ConfigurationSection
{
    private const string SectionName = "trackLog";

    public TrackConfigSection()
    {
        base[""] = new TrackConfigCollection();
    }

    [ConfigurationProperty("", IsRequired = true, IsKey = false, IsDefaultCollection = true)]
    public TrackConfigCollection TrackConfigs
    {
                get { return ((TrackConfigCollection)(base[""])); }
                set { base[""] = value; }
    }

    public static TrackConfigCollection GetTrackConfigs()
    {
        return ((TrackConfigSection)ConfigurationManager.GetSection(SectionName)).TrackConfigs;
    }
}
public class TrackConfigEntry : ConfigurationElement
{
    [ConfigurationProperty("key", IsRequired = true)]
    public string Key 
    { 
        get { return (string)base["key"]; } 
    }

    [ConfigurationProperty("env", IsRequired = true)]
    public string env 
    { 
        get { return (string)base["env"]; } 
    }

    [ConfigurationProperty("url", IsRequired = true)]
    public string url 
    { 
        get { return (string)base["url"]; } 
    }
}
var envName = System.Configuration.ConfigurationManager.AppSettings["env"];
// the envName is in the tags in the app.config
if (TrackConfigSection.GetTrackConfigs().Contains(envStringName))
{
string temp1 = TrackConfigSection.GetTrackConfig("key").ToString(); // variable to get key
                    string temp2 = TrackConfigSection.GetTrackConfig("env").ToString(); // variable to get env
                    string temp3 = TrackConfigSection.GetTrackConfig("url").ToString(); // variable to get url
}

除了@wal关于在configSection中修复程序集名称的评论之外。您还应该重写
TrackConfigCollection
类中的
ElementName
属性。将以下内容添加到
TrackConfigCollection
类中:

protected override string ElementName
{
    get { return ItemName; }
}
有关自定义配置的更多信息,请参阅优秀文章

编辑

var key = "DEV1"; 
if(TrackConfigSection.GetTrackConfigs().Contains(key))
{
    var config = TrackConfigSection.GetTrackConfigs()[key];
    var env = config.env;
    var url = config.url;
}

类型正确吗?这似乎不是一个正确的程序集/类型名称模式。您是指“我有一个“跟踪”名称空间,其他类使用它吗?是正确的程序集名称吗?即,不是
type=“Tracking.TrackConfigSection,TrackConfigSection”
而是
type=“Tracking.TrackConfigSection,name\u of\u程序集“
其中程序集的名称是
TrackConfigSection
所在的程序集,该程序集的末尾没有.dll,但仍然无法工作?你有什么例外吗?没错,它仍然不起作用,我问题末尾的那段代码看起来有效吗?对我来说似乎有效。来自我的示例的代码:var env=“DEV1”;var config=TrackConfigSection.GetTrackConfigs().Contains(env);是的,if语句是有效的,但是我如何提取带有key=“DEV1”的标记的“env”和“url”?(如要使用的正确函数)