我目前正在编写一个新的 NuGet 包,但我无法正确获取 app.config.install.xdt 文件(这是转换 app.config 以适应已安装的软件包)。
问题是在 app.config 中插入
它必须是第一个子项,否则应用程序将因异常而失败(Microsoft 强制执行)。
如果我只使用常规的“InsertIfMissing”转换,则插入将发生在任何现有子项之后,因此这似乎是行不通的。
我可以做什么来解决我的问题?
最佳答案
我遇到了完全相同的问题并通过以下方式解决了它:
<configSections xdt:Transform="InsertBefore(/configuration/*[1])" />
<configSections xdt:Locator="XPath(/configuration/configSections[last()])">
do_your_stuff_with_sections_here...
</configSections>
<configSections xdt:Transform="RemoveAll" xdt:Locator="Condition(count(*)=0)" />
第一行无条件地将节点创建为第一个子节点:
<configSections xdt:Transform="InsertBefore(/configuration/*[1])" />
第二行确保所有编辑都完成到最后一个 configSections 节点,如果该节点已经存在,则该节点是不正确的...
<configSections xdt:Locator="XPath(/configuration/configSections[last()])">
在 de configSections block 中进行转换后,输入一个命令来删除所有空的 configSections 节点...(最后一行)
<configSections xdt:Transform="RemoveAll" xdt:Locator="Condition(count(*)=0)" />
关于visual-studio-2017 - 如何在 app.config 中插入 <configSections> 作为第一个子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51761352/