Search code examples
msbuild

msbuild array iteration


<ItemGroup>
    <!-- Unit Test Projects-->
    <MyGroup Include="Hello.xml" />
    <MyGroup Include="GoodBye.xml" />     
</ItemGroup>

How do I make a task that iterates through this list and does something?

<XmlPeek XmlInputPath="%(MyGroup.Identity)"
         Query="/results">
    <Output TaskParameter="Result"
            ItemName="myResult" />
</XmlPeek>

I want to thow an error message if myresult has a certain text inside of it. However for the life of me I can't figure out how to iterate through arrays in Msbuild... anyone know how to accomplish this?


Solution

  • You could use batching on an inner target, like that :

    <ItemGroup>
      <!-- Unit Test Projects-->
      <MyGroup Include="Hello.xml" />
      <MyGroup Include="GoodBye.xml" />     
    </ItemGroup>
    
    <Target Name="CheckAllXmlFile">
      <!-- Call CheckOneXmlFile foreach file in MyGroup -->
      <MSBuild Projects="$(MSBuildProjectFile)"
               Properties="CurrentXmlFile=%(MyGroup.Identity)"
               Targets="CheckOneXmlFile">
      </MSBuild>
    </Target>
    
    <!-- This target checks the current analyzed file $(CurrentXmlFile) -->
    <Target Name="CheckOneXmlFile">
      <XmlPeek XmlInputPath="$(CurrentXmlFile)"
               Query="/results/text()">
        <Output TaskParameter="Result" ItemName="myResult" />
      </XmlPeek>
    
      <!-- Throw an error message if Result has a certain text : ERROR -->
      <Error Condition="'$(Result)' == 'ERROR'"
             Text="Error with results $(Result)"/> 
    </Target>