2015-12-23 71 views
0

我在我的.csproj文件如何使用PowerShell

<?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> 

以下刪除是我的代碼,但它給了我一個錯誤 - 「異常調用‘removeChild之’與‘1’的說法(S):‘要刪除的節點不是此節點的孩子’,」

[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) 
      } 


     } 

是否有人可以幫助我。

回答

1

$pfile是XML的根節點。您需要獲取對<ItemGroup>元素的引用,並從中刪除<Reference>節點。

可能$reference.ParentNode.RemoveChild($reference)

+0

這工作得很好,現在我看到了我犯的錯誤。謝謝! – user1234