如何将 VS2010 web.config 转换应用到具有命名空间属性的元素?

发布于 2024-09-05 21:52:54 字数 1511 浏览 3 评论 0原文

我想使用新的 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>
...

我尝试了以下转换但没有成功:

<?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-configuration 元素的 xmlns 属性中。

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

I'd like to use the new VS2010 web.config transformation feature to change the connection string within the nhibernate configuration in my web.config file. The relevant snippet is something like this:

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

I've tried the following transformation without success:

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

The problem seems to be in the xmlns attribute of the nhibernate-configuration element.

What should be the correct transformation to replace (test connection string) with (production connection string) during deployment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-09-12 21:52:54

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

<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 文件并正确命名所有节点)

I recently encountered the same issue - it was solved by placing explicit namespace prefixes in the transform file

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

The resulting transformed web.config file was thankfully free of the namespace prefixes
(i.e. it left the nhibernate namespace declaration in the same spot as it was in the
original web.config file and correctly named all the nodes)

泛泛之交 2024-09-12 21:52:54

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

您需要将 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>

The answer may be a bit late, but since I needed this as well, I figured I would post an answer that worked for me in the event anyone else stumbles upon this question.

You need to use the xdt:Locator in combination with an xpath expression to get the correct node. So something like this should work.

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

There may be a better xpath expression, but this is what worked for me.

The only issue, which isn't that big a deal, is the replaced node will have a namespace redeclared on the node. So the replaced node will actually look like this in the final output.

<property name="connection.connection_string" xmlns="urn:nhibernate-configuration-2.2">(production connection string)</property>
假装不在乎 2024-09-12 21:52:54

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

connection.connection_string_name

而不是此属性:

connection.connection_string

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

例如在 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>

If all you are trying to do is transform the connection string, do not use the transformation mechanism. Instead, in your web.config or app.config, reference this property

connection.connection_string_name

instead of this one:

connection.connection_string

This allows you to reference the connection string defined in the ConnectionStrings section, which is transformed in the usual way.

eg in web.config, use this code:

<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>
小伙你站住 2024-09-12 21:52:54

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

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

Since session-factory contains a collection of child elements, you need to tell it which child to replace using the Match locator.

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文