views:

57

answers:

1

How can I get the connection.connection_string value from the following hibernate xml file using linq?

<?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="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
     <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
     <property name="connection.connection_string">Data Source=temp.sdf</property>
     <property name="connection.release_mode">auto</property>
     <property name="show_sql">false</property>
     <property name="adonet.batch_size">500</property>
     <property name="cache.provider_class">NHibernate.Caches.SysCache2.SysCacheProvider, NHibernate.Caches.SysCache2</property>
     <property name="cache.use_query_cache">true</property>
     <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
   </session-factory>
</hibernate-configuration>

Thank you!

+1  A: 
    public string GetConnectionStringFromHibernateConfiguration(string uri)
    {
        XElement root = XElement.Load(uri);
        XNamespace ns = XNamespace.Get("urn:nhibernate-configuration-2.2");
        var query = from prop in root.Descendants(ns + "property")
                    where prop.Attribute("name").Value == "connection.connection_string"
                    select prop.Value;
        var connectionString = query.SingleOrDefault();
        return connectionString;
    }
JohnRudolfLewis
Worked beautifully! Thanks!
Chris Conway