Xmlpoke 添加多个 appsettings 密钥不起作用

Xmlpoke to add multiple appsettings key doesn't work

我有一个 Msbuild 目标,它通过将一个名为 'ProjectID' 的键添加到 web.config 的应用程序设置部分而成功执行。现在我通过向同一应用程序设置部分再添加一个键 'ApplicationId' 来更改此目标的行为。日志显示为两者都执行了 xmlpoke。但只有 projectID 值被正确替换为每个 运行.

(摘自)PropertyGroup 定义:

<?xml version="1.0" encoding="utf-8" ?>
   <Project ToolsVersion="15.0" DefaultTargets="Build" 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
   <!-- Needs to be set! -->
   <ProjectID>4424cc12-4368-45ad-ad5b-19e821eb91d4</ProjectID>
   </PropertyGroup>

目标代码:

<Target Name="UpdateConfigFilesInSolutionDir">
  <ItemGroup>
  <WebConfigFilesSolutionDir Include="$(SolutionDir)\**\*.config" />
  </ItemGroup>
  <Message Text="WebConfigFilesPath: %(WebConfigFilesSolutionDir.FullPath)" 
   Importance="high"></Message>
  <XmlPoke XmlInputPath="%(WebConfigFilesSolutionDir.FullPath)" 
   Query="//appSettings/add[@key='ProjectID']/@value" Value="$(ProjectID)" 
  />
  <XmlPoke XmlInputPath="%(WebConfigFilesSolutionDir.FullPath)" 
   Query="//appSettings/add[@key='ApplicationId']/@value" Value="SetAValue" 
  />
</Target>

观察到的输出日志:

Using "XmlPoke" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". 2019-06-25 08:37:13,202 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca] - Task "XmlPoke" 2019-06-25 08:37:13,203 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca] - Replaced "value" with "4424cc12-4368-45ad-ad5b-19e821eb91d4". 2019-06-25 08:37:13,203 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca] - Made 1 replacement(s). 2019-06-25 08:37:13,204 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca] - Done executing task "XmlPoke". 2019-06-25 08:37:13,204 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca] - Task "XmlPoke" 2019-06-25 08:37:13,204 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca] - Made 0 replacement(s). 2019-06-25 08:37:13,204 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca] - Done executing task "XmlPoke".

目前我尝试过的:

  1. 最初是在 PropertyGroup 中传递 ApplicationId 值。它 没有帮助。
  2. 所以硬编码代码中看到的值, Value="SetAValue" 仍然没有将密钥添加到 appsetting.

原来 'Add' 在 xpath 查询语法中没有添加键。相反,如果键存在,它会添加或替换键的值。虽然这不能解决我在构建期间添加应用设置密钥的问题,但它至少让我清楚了 'add' 的功能 有用的链接: http://sedodream.com/2011/12/29/UpdatingXMLFilesWithMSBuild.aspx http://samirvaidya.blogspot.com/2015/04/updating-webconfig-or-appconfig-file.html https://deejaygraham.github.io/2015/01/12/updating-web.config-settings-with-msbuild/

How to add a new key to the web.config during build(我原来问题的答案)

A combination of XmlPeek and XmlPoke made it almost work, but with more work on sanitizing the data that gets written.

<Target Name="AddApplicationNodesInConfig">
<ItemGroup>
    <WebConfigFiles Include="$(SolutionDir)\**\Web.config" />
</ItemGroup>
<Message Text="WebConfigFilesPath: %(WebConfigFiles.FullPath)" Importance="high"></Message> 
<!--read applicationId and applicationName nodes from web.config if it exists-->
  <XmlPeek XmlInputPath="%(WebConfigFiles.FullPath)" Query="//appSettings/add" >
      <Output TaskParameter ="Result" PropertyName="Peeked" />        
  </XmlPeek>
  <Message Text="From Peek: $(Peeked)"></Message>
<!--Load new node into Property-->
  <PropertyGroup>         
      <ApplicationId>ApplicationId</ApplicationId>
      <ApplicationIdVal>100</ApplicationIdVal>
      <NewNode>&lt;add key&#61;&quot;$(ApplicationId)&quot; value&#61;&quot;$(ApplicationIdVal)&quot; /&gt;</NewNode>                         
      <!-- Concatenate existing and new node into a Property -->
      <ConcatenatedNodes>$(Peeked)$(NewNode)</ConcatenatedNodes>          
      <!--in the concatenatednode, remove semicolon-->
      <ChangedPeek>$(ConcatenatedNodes.Replace(";",""))</ChangedPeek>         
  </PropertyGroup>
  <Message Text="New pacakges: $(ChangedPeek)"></Message>
<!-- Replace existing nodes with concatenated nodes-->
 <XmlPoke XmlInputPath="%(WebConfigFilesSolutionDir.FullPath)" Query="//appSettings" Value="$(ChangedPeek)" />

向 web.config 添加新密钥的唯一有用参考是 How can I use MSBuild 'afterbuild' tasks to edit a .config file? 我的解决方案基于此。