【问题标题】:Transform config file without key or name attributes转换没有键或名称属性的配置文件
【发布时间】:2023-03-20 22:35:01
【问题描述】:

我想转换一些属性,但我无法添加名称或键属性来使用 xdt:Locator="Match(name)" 或 xdt:Locator="Match(key)"。

例如,我可以:

<parentElement>
    <children>
        <add key="ExampleKey">
            <thing attribute="blablabla"></thing>
        </add>
    </children>
</parentElement>

如何将事物属性“blablabla”替换为另一个值?

更新 1:除此之外,我不能在“事物”标记中插入其他属性,因为它会通过使用该属性的 dll 引发一些错误。

更新 2:看来我必须使用 SlowCheetah,然后它运行良好。感谢预览,我发现我可以将 xdt:Transform="Replace" 添加到父元素,就像这样:

<parentElement xdt:Transform="Replace">
    <children>
        <add key="ExampleKey">
            <thing attribute="blablabla"></thing>
        </add>
    </children>
</parentElement>

现在它可以完美运行了! :)

【问题讨论】:

    标签: asp.net .net web-config app-config slowcheetah


    【解决方案1】:

    您可以使用带有Condition 定位符而不是Match 的XPath 表达式来查找元素,如documentation 中所述。即

    <?xml version="1.0" encoding="utf-8"?>
    <parentElement xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <children>
        <add>
          <thing attribute="falala" xdt:Transform="SetAttributes" xdt:Locator="Condition(@attribute = 'blablabla')"></thing>
        </add>
      </children>
    </parentElement>
    

    【讨论】:

    • 谢谢!为什么不呢,但不幸的是,在我的示例中,我无法修改 thing 元素,因为它在使用属性的 DLL 中引发错误。我更新了我的问题。
    • @LéoDavesne 不确定我是否理解 - 我的答案中的代码在您的转换文件中 - 例如Web.Release.config - 生成的转换文件不应添加任何其他属性。我可能没有完全理解您的问题,因此您可能需要添加有关您的设置的更多详细信息。例如这是 Web.config 转换,还是使用 SlowCheetah 的其他文件,还是其他?
    • 好的,所以我终于使用了 SlowCheetah,我可以预览转换。所以我可以可视化我的测试并发现我可以将 xdt:Transform="Replace" 添加到父元素。谢谢你的帮助!我会更新问题。
    猜你喜欢
    • 2016-11-08
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 2012-09-19
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多