2009-09-01 87 views
0

如何从下面的hibernate xml文件中使用linq获取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

工作得很漂亮!谢谢! – 2009-09-01 13:39:50