How can I use MSBuild 'afterbuild' tasks to edit a .config file?

≯℡__Kan透↙ 提交于 2019-12-05 18:28:09

I had the same problem. I found the solution here.

The problem is than XmlPoke considers semicolon as a value separator.

Should replace this:

<NewNode>&lt;package id&#61;&quot;$(WidgetName)&quot; version&#61;&quot;$(WidgetVersion)&quot; /&gt;</NewNode>

With:

<NewNode>&lt%3Bpackage id&#61%3B&quot%3B$(WidgetName)&quot%3B version&#61%3&quot%3$(WidgetVersion)&quot%3 /&gt%3</NewNode>

Must replace each semicolon by the secuence %3B

Here is a way to do it using MSBuild Extension Pack.

Set the packages and versions in the NewPackage item group and it adds them to the XML file.

<Project 
    ToolsVersion="4.0" 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks" />

  <Target Name="Test" DependsOnTargets="AddPackage">
  </Target>

  <ItemGroup>
    <NewPackage Include="CarDataWidget">
        <Version>3.0.0.0</Version>
    </NewPackage>
    <NewPackage Include="FooBarWidget">
        <Version>1.2.3.4</Version>
    </NewPackage>
  </ItemGroup>

  <Target Name="AddPackage">

    <PropertyGroup>
        <InputFile>in.xml</InputFile>
        <OutputFile>out.xml</OutputFile>
    </PropertyGroup>

    <Copy SourceFiles="$(InputFile)" DestinationFiles="$(OutputFile)" />

    <MSBuild.ExtensionPack.Xml.XmlFile
      TaskAction="AddElement"
      File="$(OutputFile)"
      XPath="//packages"
      Element="package"
      Key="id"
      Value="%(NewPackage.Identity)" />

    <MSBuild.ExtensionPack.Xml.XmlFile
      TaskAction="AddAttribute"
      File="$(OutputFile)"
      XPath="//packages/package[@id='%(NewPackage.Identity)']"
      Key="version"
      Value="%(NewPackage.Version)" />
  </Target>
</Project>

Take a look at my blog post http://sedodream.com/2011/12/29/UpdatingXMLFilesWithMSBuild.aspx which compares the following methods.

  1. Use SlowCheetah to transform the files for you
  2. Use the TransformXml task directly
  3. Use the built in (MSBuild 4.0) XmlPoke task
  4. Use a third party task library

Not hoping to wake up an old thread.I had the exact scenario were I had to add new keys to the appsettings section of web.config. I started off with OPs code and was stuck with the same problem with ; in the peeked value preventing the new concatenated value to be written. I fixed it by using Replace function to remove the ;

          <ConcatenatedNodes>$(Peeked)$(NewNode)</ConcatenatedNodes>          
          <!--in the concatenatednode, remove semicolon-->
          <ChangedPeek>$(ConcatenatedNodes.Replace(";",""))</ChangedPeek>
          <!-- Replace existing nodes with concatenated nodes-->
          <XmlPoke XmlInputPath="%(WebConfigFilesSolutionDir.FullPath)" Query="//appSettings" Value="$(ChangedPeek)" />

For the complete answer on how to add a new key to appsetting section of webconfig using MSBuild refer https://stackoverflow.com/a/56760009/6664129

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!