【问题标题】:Web.config transformation and search and replaceWeb.config 转换和搜索替换
【发布时间】:2012-02-15 00:54:06
【问题描述】:

我需要在 web.config 中切换多个 WCF 服务中的 IP 地址。通过 web.config 转换,除了通过 xpath 指定每个地址之外,还有什么方法可以创建搜索和替换语句。例如。对于 1.2.3.4 的所有实例,将 IP 地址 1.2.3.4 切换为 4.3.2.1

【问题讨论】:

  • 这一定是可能的......有人吗?

标签: web-config web-config-transform config-transformation


【解决方案1】:

假设您的 Web.config 是这样的(一个简化的场景,但 // 在 XPath 中适用于任何地方):

<configuration>
    <endpoint address="1.2.3.4" />
    <endpoint address="1.2.3.4" />
    <endpoint address="1.2.3.4" />
    <endpoint address="1.2.3.4" />
</configuration>

那么你将需要这样的东西:

<?xml version="1.0"?>    
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->    
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

    <replaceAll>
        <endpontAddresses xdt:Locator="XPath(//endpoint[@address='1.2.3.4'])" xdt:Transform="SetAttributes(address)" address="4.3.2.1" />
    </replaceAll>

</configuration>

注意:此 XPath 将查找整个 Web.config 中的每个 元素,并检查给定元素是否具有值等于“1.2.3.4”的地址属性。 如果您需要更通用的内容,请尝试以下操作:

<?xml version="1.0"?>
    <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->    
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

        <replaceAll>
            <endpontAddresses xdt:Locator="XPath(//*[@address='1.2.3.4'])" xdt:Transform="SetAttributes(address)" address="4.3.2.1" />
        </replaceAll>

    </configuration>

这将查看每个 XML 元素(由于星号:*)并检查它是否具有值等于“1.2.3.4”的地址属性。 所以这适用于这样的文件:

<configuration>
    <endpoint name="serviceA" address="1.2.3.4" />
    <endpoint name="serviceB" address="1.2.3.4" />
    <endpoint name="serviceC" address="1.2.3.4" />
    <endpoint2 address="1.2.3.4" />
    <endpoint3 address="1.2.3.4" />
    <endpoint4 address="1.2.3.4" />

    <innerSection>
    <endpoint address="1.2.3.4" />
    <anotherEndpoint address="1.2.3.4" />
    <sampleXmlElement address="1.2.3.4" />
    </innerSection>
</configuration>

现在,如果您想将替换限制在某个部分,即&lt;system.serviceModel&gt;,那么您可以使用这样的 XPath:

    <endpontAddresses xdt:Locator="XPath(/configuration/system.serviceModel//*[@address='1.2.3.4'])" xdt:Transform="SetAttributes(address)" address="4.3.2.1" />

这将仅更新&lt;system.serviceModel&gt; 部分中的地址

<configuration>
    <endpoint name="serviceA" address="1.2.3.4" />
    <endpoint name="serviceB" address="1.2.3.4" />
    <endpoint name="serviceC" address="1.2.3.4" />
    <endpoint2 address="1.2.3.4" />
    <endpoint3 address="1.2.3.4" />
    <endpoint4 address="1.2.3.4" />

    <innerSection>
    <endpoint address="1.2.3.4" />
    <anotherEndpoint address="1.2.3.4" />
    <sampleXmlElement address="1.2.3.4" />
    </innerSection>

    <system.serviceModel>
    <endpoint name="serviceB" address="1.2.3.4" />
    <endpoint name="serviceC" address="1.2.3.4" />
    <endpoint2 address="1.2.3.4" />
    <innerSection>
      <endpoint address="1.2.3.4" />
      <anotherEndpoint address="1.2.3.4" />
      <sampleXmlElement address="1.2.3.4" />
      </innerSection>
    </system.serviceModel>

</configuration>

试一试,选择最适合您需求的。

注意:这有一个限制,您需要指定包含 IP (1.2.3.4) 的属性的名称,但我认为最好是明确的,而不是在这里发生魔法。如果您有许多属性名称,请重复

【讨论】:

  • 您可以选择使用另一个 MSBuild 任务在文件中进行搜索和替换。执行此操作的任务是来自 MSBuildExtensionPack 的文件任务(可在 codeplex.com 获得)
  • 如何替换“”之间的值而不是这样的属性?
猜你喜欢
  • 2020-07-20
  • 1970-01-01
  • 2014-09-13
  • 2019-05-05
  • 2011-02-20
  • 2012-12-25
  • 2010-10-05
  • 1970-01-01
相关资源
最近更新 更多