Web.config转换和搜索替换

5
我需要在多个WCF服务的web.config文件中切换IP地址。使用web.config转换,除了通过xpath指定每个地址外,是否存在创建搜索和替换语句的方法。例如,将所有1.2.3.4实例的IP地址切换为4.3.2.1。

这一定在某种程度上是可能的... 有人吗? - jaspernygaard
1个回答

4

假设你的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>

现在,如果您想将替换限制为特定的部分,例如<system.serviceModel>,则可以使用以下XPath:
    <endpontAddresses xdt:Locator="XPath(/configuration/system.serviceModel//*[@address='1.2.3.4'])" xdt:Transform="SetAttributes(address)" address="4.3.2.1" />

这将仅更新 <system.serviceModel> 部分中的地址。
<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中的File任务来执行此操作(可在codeplex.com上获得)。 - Piotr Owsiak
你如何替换 "<><>" 中间的值,而不是像这样替换属性? - red888

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接