Xmlpoke添加多个appsettings键不起作用 [英] Xmlpoke to add multiple appsettings key doesn't work

查看:112
本文介绍了Xmlpoke添加多个appsettings键不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Msbuild目标,可以通过在web.config的appsetting部分中添加一个名为"ProjectID"的键来成功执行该目标.现在,通过在同一appsetting部分中再添加一个键"ApplicationId"来更改此目标的行为.日志显示了两者都执行了xmlpoke.但是每次运行都只正确替换了projectID值.

I have a Msbuild target that gets executed successfully by adding a single key called 'ProjectID' to the appsetting section of web.config. Now am changing the behavior of this target by adding one more key 'ApplicationId' to the same appsetting section. The log shows the xmlpoke is executed for both. But only the projectID value is correctly replaced with every run.

(摘录自)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>

观察到输出日志:

使用程序集"Microsoft.Build.Tasks.Core,版本= 15.1.0.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a"中的"XmlPoke"任务. 2019-06-25 08:37:13,202 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca]-任务"XmlPoke" 2019-06-25 08:37:13,203 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca]-将值"替换为"4424cc12-4368-45ad-ad5b-19e821eb91d4". 2019-06-25 08:37:13,203 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca]-进行了1次替换. 2019-06-25 08:37:13,204 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca]-执行任务"XmlPoke". 2019-06-25 08:37:13,204 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca]-任务"XmlPoke" 2019-06-25 08:37:13,204 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca]-进行了0次替换. 2019-06-25 08:37:13,204 [9] DEBUG EP.BuildService.Handlers.ProjectBuildLogger [2e0de09a-3fd8-4932-bc1d-e3a66dd3c1ca]-已执行任务"XmlPoke" .

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中.
  1. Originally was passing ApplicationId value in PropertyGroup. It didn't help.
  2. So hardcoding the value as seen in the code , Value="SetAValue" still not adding the key to appsetting.

推荐答案

事实证明,xpath查询语法中的添加"没有添加键.而是添加或替换键的值(如果存在).虽然这不能解决我在构建过程中添加应用设置键的问题,但至少可以使我清楚地了解添加"功能 有用的链接: 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/

It turns out 'Add' in the xpath Query syntax doesn't add a key. Rather it adds or replaces values for the key if it exists. While this doesn't solve my problem of adding a app setting key during build, it at least gave me clarity on the capability of 'add' Useful links: 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/

如何在构建期间向web.config添加新密钥(我的原始问题的答案)

XmlPeek和XmlPoke的组合使它几乎可以工作,但是在清理所写入的数据方面还需要做更多的工作.

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的唯一有用参考是我的解决方案基于此.

The sole useful reference for adding a new key to web.config is How can I use MSBuild 'afterbuild' tasks to edit a .config file? My solution is built on top of this.

这篇关于Xmlpoke添加多个appsettings键不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆