visual-studio-2017 - 如何在 app.config 中插入 <configSections> 作为第一个子项

标签 visual-studio-2017 nuget app-config xdt-transform xdt

我目前正在编写一个新的 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/

相关文章:

c# - VS 默认空白应用程序不是空白应用程序。如何创建一个真正的空白应用程序?

.net - NuGet:远程服务器返回错误(403)禁止

c# - 导入带有错误 : Package does not support any target frameworks 的简单自定义 NuGet 包

.net - 是否可以以编程方式执行程序集重定向

c# - XML 配置中的变量

c++ - 我需要使用 ref 和 make_pair 来返回引用吗?

c++ - 多次定义符号

visual-studio-2017 - Visual Studio 2017 : how to include NuGet packages in solution folder

nhibernate - 使用 Nuget 升级 NHibernate 和 NHibernate Validator

c# - ConfigurationElementCollection 具有许多不同类型的 ConfigurationElements