Reputation: 97
I have the following in my .csproj file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" ... ">
...
</PropertyGroup>
<PropertyGroup Condition="...">
...
</PropertyGroup>
<ItemGroup>
...
<Reference Include="abc, Version=5.0.414.0, Culture=neutral, PublicKeyToken=..., processorArchitecture=...">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\xxx\xxx\5.0\abc.dll</HintPath>
</Reference>
<Reference Include="def, Version=5.0.414.0, Culture=neutral, PublicKeyToken=..., processorArchitecture=...">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\xxx\xxx\5.0\def.dll</HintPath>
</Reference>
<Reference Include="ghi, Version=5.0.414.0, Culture=neutral, PublicKeyToken=..., processorArchitecture=...">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\xxx\xxx\5.0\ghi.dll</HintPath>
</Reference>
...
</ItemGroup>
<ItemGroup>
...
</ItemGroup>
</Project>
I have a list of DLLs in an array. I would like to find the DLLs of the array matching the DLL name in HintPath, and delete the corresponding Reference node if a match is found.
For example, if the array has abc.dll, then I would like the following to be deleted from the .csproj
<Reference Include="abc, Version=5.0.414.0, Culture=neutral, PublicKeyToken=..., processorArchitecture=...">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\xxx\xxx\5.0\abc.dll</HintPath>
</Reference>
Following is the code that I have, but it gives me an error - "Exception calling "RemoveChild" with "1" argument(s): "The node to be removed is not a child of this node.""
[xml] $pFile = Get-Content somefile.csproj
foreach ($dll in $DLLarray)
{
$ns = New-Object System.Xml.XmlNamespaceManager -ArgumentList $pFile.NameTable
$ns.AddNamespace('ns', 'http://schemas.microsoft.com/developer/msbuild/2003')
$nodes = $pFile.SelectNodes('//ns:HintPath', $ns)
foreach($node in $nodes)
{
$reference = $node.ParentNode
$str = $node.get_innerXml()
$regex = [regex] '(?is)(?<=\\)[^\\]+\.dll\b'
$allmatches = $regex.Match($str)
if ($dll -cmatch $allmatches)
{
$pFile.RemoveChild($reference)
$pFile.Save($path)
}
}
Can someone please help me out.
Upvotes: 0
Views: 910
Reputation: 7638
$pfile
is the root node of the XML. You will need to get a reference to the <ItemGroup>
element and remove the <Reference>
node from there.
Possibly $reference.ParentNode.RemoveChild($reference)
.
Upvotes: 1