Visual studio 2010 如何将VS2010 web.config转换应用于具有名称空间属性的元素?

Visual studio 2010 如何将VS2010 web.config转换应用于具有名称空间属性的元素?,visual-studio-2010,web-config,transformation,Visual Studio 2010,Web Config,Transformation,我想使用新的VS2010 web.config转换功能来更改web.config文件中nhibernate配置中的连接字符串。相关代码段如下所示: <?xml version="1.0"?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHib

我想使用新的VS2010 web.config转换功能来更改web.config文件中nhibernate配置中的连接字符串。相关代码段如下所示:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
      <property name="connection.connection_string">(test connection string)</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
...

NHibernate.Connection.DriverConnectionProvider
NHibernate.Driver.OracleDataClientDriver
(测试连接字符串)
NHibernate.ByteCode.Castle.proxyFactory,NHibernate.ByteCode.Castle
...
我尝试了以下转换,但没有成功:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
        <session-factory>
            <property name="connection.connection_string" xdt:Transform="Replace">(production connection string)</property>
        </session-factory>
    </hibernate-configuration>
</configuration>

(生产连接管柱)
问题似乎出在nhibernate配置元素的xmlns属性中


在部署期间,用(生产连接字符串)替换(测试连接字符串)的正确转换应该是什么?

由于会话工厂包含子元素的集合,您需要使用匹配定位器告诉它要替换哪个子元素

<?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
            <session-factory>
                <property name="connection.connection_string" xdt:Transform="Replace" xdt:Locator="Match(name)>(production connection string)</property>
            </session-factory>
        </hibernate-configuration>
    </configuration>


答案可能有点晚,但因为我也需要这个,我想我会发布一个对我有用的答案,以防其他人偶然发现这个问题

您需要结合使用xdt:Locator和xpath表达式来获得正确的节点。所以像这样的事情应该行得通

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   <session-factory>
      <property name="connection.connection_string" xdt:Locator="XPath(//*[local-name()='hibernate-configuration']//*[local-name()='property'][@name='connection.connection_string'])" xdt:Transform="Replace">(production connection string)</property>
   </session-factory>
</hibernate-configuration>

(生产连接管柱)
可能有更好的xpath表达式,但这对我来说是有效的

唯一的问题是,被替换的节点将在节点上重新声明名称空间,这并不是什么大问题。因此,在最终输出中,替换的节点实际上是这样的

<property name="connection.connection_string" xmlns="urn:nhibernate-configuration-2.2">(production connection string)</property>
(生产连接字符串)

我最近遇到了同样的问题-通过在转换文件中放置显式名称空间前缀解决了这个问题

<configuration
               xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
               xmlns:hib="urn:nhibernate-configuration-2.2"
              >
    <hib:hibernate-configuration>
        <hib:session-factory>
            <hib:property name="connection.connection_string" xdt:Transform="Replace">(production connection string)</hib:property>
        </hib:session-factory>
    </hib:hibernate-configuration>
</configuration>

(生产连接管柱)
谢天谢地,转换后的web.config文件没有名称空间前缀 (即,它将nhibernate名称空间声明保留在与
原始web.config文件,并正确命名所有节点)

如果您只想转换连接字符串,请不要使用转换机制。相反,在web.config或app.config中,引用此属性

connection.connection_string_name
而不是这个:

connection.connection_string
这允许您引用ConnectionString部分中定义的连接字符串,该字符串以通常的方式进行转换

例如,在web.config中,使用以下代码:

<connectionStrings>
  <add name="DefaultConnection" connectionString="server=MYSERVER; Integrated Security=SSPI; database=MYDATABASE"/>
</connectionStrings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string_name">DefaultConnection</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    <property name="current_session_context_class">web</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

NHibernate.Connection.DriverConnectionProvider
NHibernate.dialogue.mssql2000方言
NHibernate.Driver.SqlClientDriver
默认连接
NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu
网状物
真的

我已经尝试过了,但是转换步骤甚至找不到“hibernate配置”节点。。。很可能是因为它的xmlns属性。我知道我在这里抓住了救命稻草,但在放弃之前,我要做的最后一件事是消除hibernate配置元素中多余的空白,以便配置完全匹配。我已经编辑了上面的代码以反映我所说的内容。我还需要添加定位器匹配以使我的工作正常:Data Source=Production。dbI已经创建了一个工具来测试配置转换,也许它会有所帮助:它对我有帮助,但被替换的元素注入了
xmlns:hib=“…”
:(