2009-09-01 17 views
0

linqを使用している以下のhibernate xmlファイルからconnection.connection_string値を取得するにはどうすればよいですか?linq to xml - 休止状態のファイルを読み取る

<?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> 

ありがとうございます!

答えて

1
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; 
    } 
+0

美しく働いた!ありがとう! –