【问题标题】:Clean-up redundant <None Remove="..."/> from .csproj file从 .csproj 文件中清理多余的 <None Remove="..."/>
【发布时间】:2018-06-18 14:15:15
【问题描述】:

我在 dotnet core 项目中复制了一个文件,在项目资源管理器中,无法更改其名称,因此将该文件从项目中排除,在文件系统中重命名,然后再次添加到具有不同名称的项目中。现在,在.csproj 文件中,有以下冗余行。

&lt;None Remove="File - Copy of FileX.js"/&gt;

我想删除它,我知道我可以手动编辑.csproj 文件,但在我看来应该有更好、更清洁、更用户友好的方式来摆脱那些多余的行.不过没找到。

谁能告诉我执行此操作的标准方法?

【问题讨论】:

    标签: visual-studio-2017 csproj


    【解决方案1】:

    我认为您最终可能会得到一个适用于 xml 节点的 powershell 脚本:

    param($path)
    $MsbNS = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}
    
    function RemoveElement([xml]$Project, [string]$XPath, [switch]$SingleNode)
    {
      $nodes = @(Select-Xml $XPath $Project -Namespace $MsbNS | Foreach {$_.Node})
      if (!$nodes) { Write-Verbose "RemoveElement: XPath $XPath not found" }
      if ($singleNode -and ($nodes.Count -gt 1)) { 
        throw "XPath $XPath found multiple nodes" 
      }
      foreach ($node in $nodes)
        $parentNode = $node.ParentNode
        [void]$parentNode.RemoveChild($node)
      }
    }
    
    $proj = [xml](Get-Content $path)
    RemoveElement $proj '//msb:None Remove' -SingleNode
    $proj.Save($path)
    

    未经测试,但应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 2018-12-17
      相关资源
      最近更新 更多