且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在app.config中添加自定义标签

更新时间:2022-11-18 19:49:49

亲爱的朋友,



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



< 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 >





供参考: - 访问(^)



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