我需要一些使用PowerShell编辑csproj文件的帮助。我基本上需要选择一个节点并改变它。
示例:
<None Include="T4\WebConfigSettingGeneratorScript.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>WebConfigSettingGeneratorScript1.txt</LastGenOutput>
</None>
我需要从此标记中删除TextTemplatingFileGenerator属性。
答案 0 :(得分:10)
我做了很多这样的事情。我保留了一组辅助函数来处理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)