C# 从C读取NHibernate配置文件#

C# 从C读取NHibernate配置文件#,c#,xml,xml-parsing,C#,Xml,Xml Parsing,在我的web应用程序中,我使用由xml文件(NHibernate.cfg.xml)配置的NHibernate。我需要在我的C代码中读取该文件中的连接字符串。我的代码正在工作,但我不喜欢它(特别是foreach!),我希望以一种直接的方式查询xml文件。我试图用link查询它,但总是得到空节点。 以下是cfg文件: <?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibern

在我的web应用程序中,我使用由xml文件(NHibernate.cfg.xml)配置的NHibernate。我需要在我的C代码中读取该文件中的连接字符串。我的代码正在工作,但我不喜欢它(特别是foreach!),我希望以一种直接的方式查询xml文件。我试图用link查询它,但总是得到空节点。 以下是cfg文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">
      NHibernate.Connection.DriverConnectionProvider
    </property>
    <property name="connection.driver_class">
      NHibernate.Driver.SqlClientDriver
    </property>
    <property name="connection.connection_string">
      Server=LEG\SQL12;initial catalog=DVR;user id=daniele;pwd=s;
    </property>
    <property name="dialect">
      NHibernate.Dialect.MsSql2008Dialect
    </property>
  </session-factory>
</hibernate-configuration>

查询xml时如何获得相同的结果?

我尝试了这个方法,但root.subjections(“property”)给了我“枚举没有结果”。
XElement root = XElement.Load("hibernate.cfg.xml");
var connString = root.Descendants("property")
             .Where(p => (string) p.Attribute("name") == "connection.connection_string")
             .Select(p => (string) p)
             .First();
XElement root = XElement.Load("hibernate.cfg.xml");
var connString = root.Descendants("property")
             .Where(p => (string) p.Attribute("name") == "connection.connection_string")
             .Select(p => (string) p)
             .First();