如何将 VS2010 web.config 转换应用到具有命名空间属性的元素?
我想使用新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最近遇到了同样的问题 - 通过在转换文件中放置显式名称空间前缀解决了问题
幸运的是,转换后的 web.config 文件没有名称空间前缀
(即它将 nhibernate 命名空间声明留在了与它在
原始 web.config 文件并正确命名所有节点)
I recently encountered the same issue - it was solved by placing explicit namespace prefixes in the transform file
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)
答案可能有点晚了,但由于我也需要这个,我想我会发布一个对我有用的答案,以防其他人偶然发现这个问题。
您需要将 xdt:Locator 与 xpath 表达式结合使用才能获取正确的节点。所以这样的事情应该有效。
可能有更好的 xpath 表达式,但这对我有用。
唯一的问题(这并不是什么大问题)是被替换的节点将在该节点上重新声明一个命名空间。因此,替换后的节点在最终输出中实际上看起来像这样。
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.
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.
如果您只想转换连接字符串,请不要使用转换机制。相反,在 web.config 或 app.config 中,引用此属性
而不是此属性:
这允许您引用 ConnectionStrings 部分中定义的连接字符串,该字符串以通常的方式进行转换。
例如在 web.config 中,使用以下代码:
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
instead of this one:
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:
由于会话工厂包含子元素的集合,因此您需要使用匹配定位器告诉它要替换哪个子元素。
Since session-factory contains a collection of child elements, you need to tell it which child to replace using the Match locator.