我的.csproj文件中有以下内容
<?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>
我有一个数组中的DLL列表。我想在HintPath中找到与DLL名称匹配的数组的DLL,如果找到匹配则删除相应的Reference节点。
例如,如果数组有abc.dll,那么我希望从.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>
以下是我的代码,但它给了我一个错误 - &#34;异常调用&#34; RemoveChild&#34;用&#34; 1&#34;参数:&#34;要删除的节点不是该节点的子节点。&#34;&#34;
[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)
}
}
有人可以帮帮我吗。
答案 0 :(得分:1)
$pfile
是XML的根节点。您需要获得对<ItemGroup>
元素的引用,并从那里删除<Reference>
节点。
可能$reference.ParentNode.RemoveChild($reference)
。