user2122779
user2122779

Reputation: 1

Parse xml inside msbuild file

I have a problem to get data from msbuild file where I have something like this:

<Application Include="ABC">
...
/Application>
<Application Include="XYZ">
        <Parameters>
                <Param name="param1">valueOfIt</Param>
                        <Param name="param2">value2</Param>
                </Parameters>
</Application>

I can rad all <Param> in <Parameters> like this:

<FindInList CaseSensitive="false" List="@(Application)" ItemSpecToFind="$(Application)"> <Output TaskParameter="ItemFound" ItemName="Parameters"/> </FindInList>

then <Message Text="@(Parameters->'%(Parameters)'))"/> displays an xml with all <Param>. I need to create a property with the value from param1... How to parse this??

Upvotes: 0

Views: 1713

Answers (1)

Palo Misik
Palo Misik

Reputation: 761

If you are interested just in static ItemGroup you can use XmlPeek (this answer is inspired by)

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

<ItemGroup>
    <Application Include="ABC">
    </Application>
    <Application Include="XYZ">
            <Parameters>
                    <Param name="param1">valueOfIt</Param>
                    <Param name="param2">value2</Param>
            </Parameters>
    </Application>
</ItemGroup>


<Target Name="Sample">
    <XmlPeek Namespaces="&lt;Namespace Prefix='msb' Uri='http://schemas.microsoft.com/developer/msbuild/2003'/&gt;"
                 XmlInputPath="$(MSBuildProjectFile)" 
                 Query="/msb:Project/msb:ItemGroup/msb:Application/msb:Parameters/msb:Param[@name='param1']/text()">
            <Output TaskParameter="Result" PropertyName="PeekedParam1" />
        </XmlPeek>
        <Message Text="'$(PeekedParam1)'" />
  </Target>
</Project>

Upvotes: 1

Related Questions