0

我们开始使用 nhibernate 并设置了一个 Session Manager 来创建一个新的 SessionFactory。我需要在应用程序第一次启动时修改一些信息。

我使用 XDocument 打开配置文件(不是 app.config)。

<settings>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <reflection-optimizer use="false"/>
    <session-factory>
       <property name="x">SomeValue</property>
    </session-factory>
  </hibernate-configuration>
</settings>

XDocument xdoc = XDocument.Load(<file>);
var x = xdoc.Root.Element("hibernate-configuration");

x 为空,除非我删除 xmlns。我错过了什么?

4

2 回答 2

3

看起来您是通过 null 命名空间中的本地名称而不是您在此处添加的新命名空间来调用该元素:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

试试这个:

xdoc.Root.Element(XName.Get("hibernate-configuration", "urn:nhibernate-configuration-2.2"))
于 2009-02-25T15:52:23.113 回答
1

您需要使用 XName.Get 传递此名称空间 URI,否则您将只能在默认的空名称空间中获得 <hibernate-configuration> 元素的匹配项。

var x = xdoc.Root.Element (
  XName.Get ( "hibernate-configuration", "urn:nhibernate-configuration-2.2" ) );
于 2009-02-25T15:49:29.267 回答