如何从 C# webconfig 小节获取属性?

Jel*_*lsa 2 c# configurationmanager web-config-transform

我尝试从一个小节中获取所有属性,但该节有很多小节并且应用程序无法识别,我该怎么办?这是我的网络配置:

<configSections>
    <section name="Seccion" type="ManejoConfiguracion.SeccionConfig,ManejoConfiguracion"/>
   </configSections>

  <Seccion>
    <BD>
    <add key="name" value="dbKey" />
    <add key="user" value="userBD" />
    <add key="pass" value="123BD" />
    </BD>

    <ReportingService>
    <add key="name" value="Reporting" />
    <add key="user" value="userReport" />
    </ReportingService>

   </Seccion>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Did*_*xis 5

呸! 忘记那些疯狂的配置对象。使用 Linq to XML 就可以了:

var seccion = 
    XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        .Root.Element("Seccion");

// Now Linq to XML until your heart's content!
var user = (string)seccion.Element("BD").Elements("add")
    .Where(x => (string)x.Attribute("key") == "user")
        .Single().Attribute("value");
Run Code Online (Sandbox Code Playgroud)