XMLFile Addelement - need to add 3 elements of same type but fails on 2nd?

Feb 6, 2012 at 10:16 AM

I'm trying to use the XMLFile TaskAction AddElement to as follows:

Before:

<RenderingConfiguration>
    <RenderEngines>
    </RenderEngines>
</RenderingConfiguration>

After

<RenderingConfiguration>
    <RenderEngines>
      <RenderEngine ID="RE1" />
      <RenderEngine ID="RE2" />
      <RenderEngine ID="RE3" />
    </RenderEngines>
</RenderingConfiguration>

I have an Itemgroup set up with the 3 entries I want to add, and I'm using the following call to add the elements:

<XmlFile TaskAction="AddElement" File="pathtoxmlfile.xml" Element="RenderEngine" ParentElement="//RenderingConfiguration/RenderEngines" Key="ID" Value=%(Itemgroup.ID) />

Running thru the code, the output shows it adding the 1st RenderEngine entry with the correct ID, but then it doesn't add the 2nd and 3rd, so the output file ends up with:

<RenderingConfiguration>
    <RenderEngines>
      <RenderEngine ID="RE1" />
    </RenderEngines>
</RenderingConfiguration>

Am I doing something wrong?

Coordinator
Feb 6, 2012 at 10:07 PM
Edited Feb 6, 2012 at 10:08 PM

You need to use Xpath. If you use addelement without an xpath then you can't add the same element more than once. This isn't too well documented. This should work for you

 

<Project ToolsVersion="4.0" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks"/>
    <ItemGroup>
        <Itemgroup Include="1">
            <ID>RE1</ID>
        </Itemgroup>
        <Itemgroup Include="2">
            <ID>RE2</ID>
        </Itemgroup>
        <Itemgroup Include="3">
            <ID>RE3</ID>
        </Itemgroup>
    </ItemGroup>
    <Target Name="Default">
        <XmlFile TaskAction="AddElement" File="d:\a\pathtoxmlfile.xml" Element="RenderEngine" ParentElement="//RenderingConfiguration/RenderEngines" XPath="//RenderingConfiguration/RenderEngines" Key="ID" Value="%(Itemgroup.ID)" />
    </Target>
</Project>
Feb 7, 2012 at 8:56 AM

Thanks Mike - that fixed the issue ;)