I am having Msbuild XML file like this.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Notifications Include="[email protected]"/>
</ItemGroup>
<ItemGroup>
<Xcopy Include="..\..\..\Release\UtilitiesAssembly.dll">
<Destination>"..\..\..\Mycomponent\depl\BOM\Folder1</Destination>
</Xcopy>
<Xcopy Include="..\..\..\Release\Core.assembly.dll">
<Destination>"..\..\..\Mycomponent\depl\BOM\Folder1</Destination>
</Xcopy>
<Xcopy Include="..\..\..\Release\UIAssembly.dll">
<Destination>"..\..\..\Anothercomponent\Folder1</Destination>
</Xcopy>
<Xcopy Include="..\..\..\Release\Core.assembly.dll">
<Destination>"..\..\..\Anothercomponent\depl\BOM</Destination>
</Xcopy>
</Itemgroup>
</Project>
Actually i would like to group XCopy Itemgroup like this
<Xcopy Include="..\..\..\Release\UtilitiesAssembly.dll;
..\..\..\Release\\Core.assembly.dll;">
<Destination>"..\..\..\Mycomponent\depl\BOM\Folder1</Destination>
</Xcopy>
<Xcopy Include="..\..\..\Release\UIAssembly.dll;">
<Destination>"..\..\..\Anothercomponent\Folder1</Destination>
</Xcopy>
<Xcopy Include="..\..\..\Release\Core.assembly.dll">
<Destination>"..\..\..\Anothercomponent\depl\BOM</Destination>
</Xcopy>
How to achieve it using Powershell or Msbuild or by some other mechanism
Here is an example how to do this using the
Group-Object
cmdlet. It will group the xcopy elements byDestination
and combine theInclude
paths into a semi-colon separated string. The output is stored in a new XML document and saved to disk. This doesn't programatically update the original. You can just copy paste the new consolidated XML over you original XML.Update Now that I see your whole file, I see it has a default namespace which causes problems with XPath. There are two ways to get around this problem.
Here is an updated example using namespace agnostic xpath. Also in order to get the indentation you are looking for we'll need to do some text file processing after the XML object processing.
Creates output: