2009-02-25 199 views
0

我们开始使用nhibernate并设置会话管理器来创建新的SessionFactory。我需要在应用程序第一次启动时修改一些信息。具有名称空间的Xml元素

我使用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。我错过了什么?

回答

3

它看起来像你调用由它的元素是从空命名空间的本地名称,而不是新的命名空间添加你的位置:

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

试试这个:

xdoc.Root.Element(XName.Get("hibernate-configuration", "urn:nhibernate-configuration-2.2")) 
1

您需要将此名称空间URI与XName.Get一起传递,否则只会在缺省空名称空间中获得匹配< hibernate-configuration>元素的匹配项。

var x = xdoc.Root.Element (
    XName.Get ("hibernate-configuration", "urn:nhibernate-configuration-2.2")); 
相关问题