如何使用PowerShell删除csproj中的元素

时间:2017-07-12 12:46:07

标签: xml powershell csproj

我尝试从<ItemGroup>文件中删除.csproj个元素,但我的脚本什么也没做。我做错了什么?

脚本:

[xml]$csproj = Get-Content -Path ".\PackageTest.csproj"

$refs = $csproj.Project.ItemGroup | Where-Object {$_.Content.Include -like "PkgFolder\managed*"} 

ForEach($ref in $refs)
{
    $csproj.Project.RemoveChild($ref)
}

$csproj.Save(".\PackageTest.csproj") 

我的csproj文件:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
#
# smthng
#      
  <ItemGroup>
    <Content Include="PkgFolder\managed1.zip">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>      
  <ItemGroup>
    <Content Include="PkgFolder\managed2.zip">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>      
  <ItemGroup>
    <Content Include="PkgFolder\managed3.zip">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>         
#
# smthng
#            
</Project>

1 个答案:

答案 0 :(得分:1)

首先:

$file = gi ".\PackageTest.csproj"
[xml]$csproj = Get-Content $file

第二

$csproj.Save($file.FullName)