2009-02-25 200 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")); 
相關問題