views:

527

answers:

2

We are starting to use nhibernate and have setup a Session Manager to create a new SessionFactory. I need to modify some information the 1st time the app starts.

I open the config file (not app.config) using an XDocument.

<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 is null unless i remove the xmlns. What am I missing?

+1  A: 

You need to pass this namespace URI with XName.Get, otherwise you will only get a match for <hibernate-configuration> elements within the default, empty namespace.

var x = xdoc.Root.Element (
  XName.Get ( "hibernate-configuration", "urn:nhibernate-configuration-2.2" ) );
baretta
+2  A: 

It looks like you're calling the element by it's local name from the null namespace rather than the new namespace you added here:

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

try this:

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