web-config – Web.config转换以及搜索和替换

web-config – Web.config转换以及搜索和替换,第1张

概述我需要在web.config中的多个WCF服务中切换出一个IP地址.使用web.config转换,除了通过xpath指定每个地址之外,还有什么方法可以创建搜索和替换语句.例如.对于1.2.3.4的所有实例,用4.3.2.1切换IP地址1.2.3.4 假设你的Web.config是这样的(一个简化的场景,但是//在XPath中无处不在): <configuration> <endpoint 我需要在web.config中的多个WCF服务中切换出一个IP地址.使用web.config转换,除了通过xpath指定每个地址之外,还有什么方法可以创建搜索和替换语句.例如.对于1.2.3.4的所有实例,用4.3.2.1切换IP地址1.2.3.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),但我认为最好是明确的,而不是在这里发生魔术.如果您有许多名称,请重复

总结

以上是内存溢出为你收集整理的web-config – Web.config转换以及搜索和替换全部内容,希望文章能够帮你解决web-config – Web.config转换以及搜索和替换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1059694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存