使用Web.Config转换插入多个项目

6
我有一个引用了许多WCF服务的C#项目。为了进行本地测试,我想要替换identity标签的内容,以便它接受任何在localhost上运行的内容。
以下转换是有效的,但只会在第一个匹配位置插入dns元素。因此,如果我有5个引用的终结点,其中一个将具有dns标记,而其他终结点则都具有空的identity元素。
<system.serviceModel>
    <client>
      <endpoint>
        <identity>
          <dns xdt:Transform="Insert" value="localhost"/>
          <userPrincipalName xdt:Transform="RemoveAll" value="someIdentity" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

如何修改所有匹配元素,而不仅仅是第一个?

你是想要移除所有的<userPrincipalName>元素吗? - Eric Falsken
是的,那部分工作得很好。然而,我还想用<dns>替换那些<userPrincipalName>元素,但我没有成功。我已经通过手动列出所有的端点来解决了这个问题,但如果有更优雅的解决方案,我很愿意听听。 - Zugbo
1个回答

2
使用xdt:Locator属性来定义一个XPath表达式,用于匹配所有您想要插入的<identity>元素。
  <system.serviceModel>
    <client>
      <endpoint>
        <identity xdt:Locator="XPath(//identity)">
          <dns xdt:Transform="Insert" value="localhost"/>
          <userPrincipalName xdt:Transform="RemoveAll"/>
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

浏览http://xdt.codeplex.com/SourceControl/latest#XmlTransform/XmlTransforms.cs的源代码后,我得出结论,目前不支持此功能。 - David Gardiner

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