1

我在 C# 应用程序中有 App.config 文件。我想使用 xdt 转换将所有与“.UAT”匹配的键部分替换为“.PROD”。

<?xml version="1.0"?>    
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<appSettings>
    <add key="MyParam1.UAT" value="param1"/>
    <add key="MyParam2.UAT" value="param2"/>
    <add key="MyParam2.UAT" value="param3"/>
  </appSettings>
</configuration>

这是我想要的输出

    <?xml version="1.0"?>    
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <appSettings>
            <add key="MyParam1.PROD" value="param1"/>
            <add key="MyParam2.PROD" value="param2"/>
            <add key="MyParam2.PROD" value="param3"/>
          </appSettings>
        </configuration>

到目前为止,我已经使用CustomTransform进行了尝试,但它只替换了一个元素而不是所有元素。我怎样才能得到它来替换所有元素

    <?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">

<appSettings>
    <add xdt:Locator="Condition(contains(@key, '.UAT'))" xdt:Transform="AttributeRegexReplace(Attribute='key', Pattern='.UAT',Replacement='.PROD')" />
  </appSettings>
</configuration>
4

1 回答 1

0

我在这里找到了答案,我必须用这种迭代所有属性的方法替换 Apply 方法

protected override void Apply()
{
    foreach (XmlNode target in this.TargetNodes)
    {
        foreach (XmlAttribute att in target.Attributes)
        {
            if (string.Compare(att.Name, this.AttributeName, StringComparison.InvariantCultureIgnoreCase) == 0)
            { // get current value, perform the Regex 
                att.Value = Regex.Replace(att.Value, this.Pattern, this.Replacement);
            }
        }
    }
}
于 2015-07-05T16:44:37.837 回答