visual-studio-2010 - 如何使用 PowerShell 编辑 .csproj 文件 - Visual Studio 2010

标签 visual-studio-2010 powershell

我需要一些帮助来使用 PowerShell 编辑 csproj 文件。我基本上需要选择一个节点并对其进行更改。

例子:

<None Include="T4\WebConfigSettingGeneratorScript.tt">
  <Generator>TextTemplatingFileGenerator</Generator>
  <LastGenOutput>WebConfigSettingGeneratorScript1.txt</LastGenOutput>
</None>

我需要从此标记中删除 TextTemplatingFileGenerator 属性。

最佳答案

我经常做这种事情。我保留了一组用于操作 XML 文件的辅助函数 - 特定的 C# 项目文件。试试这个:

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/msb:Generator' -SingleNode
$proj.Save($path)

关于visual-studio-2010 - 如何使用 PowerShell 编辑 .csproj 文件 - Visual Studio 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6034054/

相关文章:

powershell - 如何在 PowerShell 代码中发送带有附件的电子邮件?

powershell - 测试字符串的最佳方法是什么?

mysql - 当我在页面加载时使用数据读取器附加文本框的内容时,更新查询不起作用

c++ - 异常的 Unresolved external symbol 错误

c++ hash 在具有相同输入的 VS 2010 和 VS 2013 上返回不同的结果

powershell - 如何通过远程服务器上的 Invoke-Command 运行多个命令

.net - MemoryStream 而不是 byte[] 作为资源添加的文件?

c# - ASP.NET 母版页解析器错误

css - Npm 多文件脚本 (lessc)

powershell - 如何使用 Function Apps 在 Azure 中使用 PowerShell 登录 AzureRmAccount