在app.config中添加自定义标签 [英] Adding custom tags in app.config

查看:308
本文介绍了在app.config中添加自定义标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我必须在app.config中添加配置标签,如何在C#中访问标签值。我已经用Google搜索并获得了一些解决方案。我试过了,得到了以下错误



每个配置文件只允许一个< configsections>元素,如果存在,则必须是root <$ p的第一个子元素$ p>< configuration>

元素。



我的App.Config是



<?xml version =1.0encoding =utf-8?> 
< configuration>
< startup>
< supportedRuntime version =v4.0sku =。NETFramework,Version = v4.5/>
< / startup>
< configSections>
< section name =Categorytype =Hang.MyConfigSection/>

< / configSections>

< Category>
< add name =AnimalsFilePath =Animals.txt/>
< add name =CitiesFilePath =Cities.txt/>
< / Category>
< / configuration>







和C#代码是

  public   class  MyConfigSection:ConfigurationSection 
{
[ConfigurationProperty( ,IsRequired = true ,IsDefaultCollection = true )]
public MyConfigInstanceCollection实例
{
get { return (MyConfigInstanceCollection) this [ ]; }
set { this [ ] = value ; }
}
}

public class MyConfigInstanceCollection :ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new MyConfigInstanceElement();
}

受保护 覆盖 object GetElementKey(ConfigurationElement element)
{
// 设置为您想要用于键的任何元素属性
return ((MyConfigInstanceElement)element).Name;
}
}

public class MyConfigInstanceElement: ConfigurationElement
{
// 确保为上面的GetElementKey公开的属性设置IsKey = true
[ConfigurationProperty( name,IsKey = true ,IsRequired = true )]
public < span class =code-keyword> string 名称
{
get { return string base [ name]; }
set { base [ name] = value ; }
}

[ConfigurationProperty( FilePath,IsRequired = true )]
public string 代码
{
获取 {返回 string base [ 文件路径]; }
set { base [ FilePath] = value ; }
}
}





并且通过

来呼叫

  var  config = ConfigurationManager.GetSection( 类别
as MyConfigSection;









我错过哪里?...





全部谢谢。

解决方案

亲爱的朋友,



您的错误说明了一切。纠正你的代码如下: -



< configuration> 
< configsections>
< section name = 类别 type = Hang.MyConfigSection />
< / configsections >
< startup>
< supportedruntime version = v4.0 sku = 。NETFramework,Version = v4.5 />
< / startup >
< category>
< add name = 动物 filepath = Animals.txt />
< add name = Cities filepath = Cities.txt />
< / 类别 >
< / configuration >





供参考: - 访问(^)



< b> configSections必须是根元素的第一个子元素:



此链接将帮助您: - 在app.config中访问自定义标签





请将它标记为您的答案,如果它可以帮助您。



问候



Varun Sareen


Hi
I have to add a configuration tag in app.config, how can i access the tag values in C#.I have already googled and got some solutions. I tried that and got the below error

"Only one <configsections> element allowed per config file and if present must be the first child of the root

<configuration>

element."

My App.Config is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <configSections>
    <section name="Category" type="Hang.MyConfigSection" />

  </configSections>

  <Category>
    <add name="Animals" FilePath="Animals.txt" />
    <add name="Cities" FilePath="Cities.txt" />
  </Category>
</configuration>




and C# Code is

public class MyConfigSection : ConfigurationSection
   {
       [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
       public MyConfigInstanceCollection Instances
       {
           get { return (MyConfigInstanceCollection)this[""]; }
           set { this[""] = value; }
       }
   }

   public class MyConfigInstanceCollection : ConfigurationElementCollection
   {
       protected override ConfigurationElement CreateNewElement()
       {
           return new MyConfigInstanceElement();
       }

       protected override object GetElementKey(ConfigurationElement element)
       {
           //set to whatever Element Property you want to use for a key
           return ((MyConfigInstanceElement)element).Name;
       }
   }

   public class MyConfigInstanceElement : ConfigurationElement
   {
       //Make sure to set IsKey=true for property exposed as the GetElementKey above
       [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
       public string Name
       {
           get { return (string)base["name"]; }
           set { base["name"] = value; }
       }

       [ConfigurationProperty("FilePath", IsRequired = true)]
       public string Code
       {
           get { return (string)base["FilePath"]; }
           set { base["FilePath"] = value; }
       }
   }



and calling by

var config = ConfigurationManager.GetSection("Category")
                 as MyConfigSection;





Where am i missing?...


Thanks All.

解决方案

Dear Friend,

Your error says it all. Correct your code like this:-

<configuration>
  <configsections>
    <section name="Category" type="Hang.MyConfigSection" />
  </configsections>
  <startup>
    <supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <category>
    <add name="Animals" filepath="Animals.txt" />
    <add name="Cities" filepath="Cities.txt" />
  </category>
</configuration>



for reference:-Visit (^)

configSections must be the first child of the root element:

This link will help you out:- Access Custom Tag in app.config


Please mark it as your answer if it helps you out.

Regards

Varun Sareen


这篇关于在app.config中添加自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆