如何从小节webconfig C#中获取属性? [英] How can I get attribute from subsection webconfig C#?
本文介绍了如何从小节webconfig C#中获取属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试从一个小节中获取所有服装,但是该节中有很多小节,并且应用程序无法识别,该怎么办?这是我的webconfig:
I try to get all attibutes from one subsection, but section have many subsection and the aplication didn't recognize, how can I do? this is my webconfig:
<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>
推荐答案
B!忘记那些疯狂的配置对象.使用Linq对其上的XML:
Bah! Forget those crazy configuration objects. Use Linq to XML on it:
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");
这篇关于如何从小节webconfig C#中获取属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文