我需要一些帮助来使用 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/