clogwog
clogwog

Reputation: 373

MSBuild search and replace in xml file

I want to automatically generate a versionCode from a versionName. I want to remove all dots . and replace X's as 00's

So from 2.05.X.20 -> 2050020

I've started tackeling the X to 00 replacement So in the VS csproj file i've added the follwoing BeforeBuild target:

  <Target Name="BeforeBuild">
      <!-- find the versionName in the file -->
      <XmlPeek XmlInputPath="Properties\AndroidManifest.xml" Namespaces="&lt;Namespace Prefix='android' Uri='http://schemas.android.com/apk/res/android' /&gt;" Query="manifest/@android:versionName">
        <Output TaskParameter="Result" ItemName="FoundVersionName" />
      </XmlPeek>

        <!-- write the replaced versioncode to the file -->
        <XmlPoke XmlInputPath="Properties\AndroidManifest.xml" Namespaces="&lt;Namespace Prefix='android' Uri='http://schemas.android.com/apk/res/android' /&gt;" Query="manifest/@android:versionCode" 
          Value="$([System.String]::Copy($(FoundVersionName)).Replace('X','00')) "               />
  </Target>

however the output of this looks like

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="$(FoundVersionName)" android:versionName="2.05.X.20" android:installLocation="auto" package="mtdata_taxi_tablet.mtdata_taxi_tablet">

i've tried

but none of them work

if i use

android:versionCode is replaced with 2.05.X.20

any thoughts ?

Upvotes: 2

Views: 1169

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100581

The syntax you used for replacement is correct, however it requires a "property" that is accessible via the $() syntax. But your <XmlPeek> task is configured to return an item. you can change this by replacing

<Output TaskParameter="Result" ItemName="FoundVersionName" />

with

<Output TaskParameter="Result" PropertyName="FoundVersionName" />

Upvotes: 3

Related Questions