1

私のカスタム設定セクションは次のようになります。

  <MySection>
    <add name="a" value="111"/>
    <add name="b" value="222"/>
    <add name="c" value="333"/>
    ...
  </MySection>

カスタム構成セクションを作成する方法は知っていますが、そのすべてのエントリを反復処理する方法はありますか?

問題の解決: http: //msdn.microsoft.com/en-us/library/system.configuration.configurationelement (v = vs.100).aspx

4

2 に答える 2

0

'GetSection'(以下のリンク)を使用します。

// Get the AppSettings section.
var sect = (MySection)ConfigurationManager.GetSection("mysection");

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.getsection.aspx

于 2012-10-14T04:14:49.453 に答える
0

If you want to parse your section use:

var config = (SampleConfigurationSection)ConfigurationManager.GetSection("sampleConfiguration");

config.YourCustomProperty = "Hello World";

If you want to iterate through the XML elements manually (which would be a really strange requirement), you could use LINQ to XML

using System.Xml.Linq;

....

var xml = XDocument.Load("path to your config file");

var section = from e in xml.Root.Elements("your section name").Elements()
              select e;

If you need dynamic items, then perhaps you could use the ConfigurationElementCollection and then parse your section and work with your collection objects instead of working with the XML directly

于 2012-10-14T04:16:58.983 に答える