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

Mar*_*sen 2 app-config nuget xdt-transform xdt visual-studio-2017

我目前正在编写一个新的 NuGet 包,但无法正确获取app.config.install.xdt文件(这是转换 app.config 以适应已安装包的 xml 文件)。

\n\n

问题是在 app.config 中插入<configSections>部分作为第一个子项 -但仅限于它丢失的情况!

\n\n

它必须是第一个子项,否则应用程序将因异常而失败(Microsoft 强制执行)。

\n\n

如果我只使用常规\xe2\x80\x9cInsertIfMissing\xe2\x80\x9d转换,则插入发生在任何现有子项之后,因此这似乎是不行的。

\n\n

我可以做什么来解决我的问题?

\n

小智 5

我遇到了完全相同的问题并通过以下方式解决了它:

\n\n
<configSections xdt:Transform="InsertBefore(/configuration/*[1])" />\n<configSections xdt:Locator="XPath(/configuration/configSections[last()])">\n     do_your_stuff_with_sections_here...\n</configSections>\n<configSections xdt:Transform="RemoveAll" xdt:Locator="Condition(count(*)=0)" />\n
Run Code Online (Sandbox Code Playgroud)\n\n

第一行无条件地将节点创建为第一个子节点:

\n\n
<configSections xdt:Transform="InsertBefore(/configuration/*[1])" />\n
Run Code Online (Sandbox Code Playgroud)\n\n

第二行确保所有编辑都完成到最后一个 configSections 节点,如果该节点已存在\xe2\x80\xa6,则该节点是不正确的节点

\n\n
<configSections xdt:Locator="XPath(/configuration/configSections[last()])">\n
Run Code Online (Sandbox Code Playgroud)\n\n

在 de configSections 块中进行转换后,输入一个命令来删除所有空的 configSections 节点\xe2\x80\xa6(最后一行)

\n\n
<configSections xdt:Transform="RemoveAll" xdt:Locator="Condition(count(*)=0)" />\n
Run Code Online (Sandbox Code Playgroud)\n