I need some help editing a csproj file using PowerShell. I basically need to select a node and alter it.
Example:
<None Include="T4\WebConfigSettingGeneratorScript.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>WebConfigSettingGeneratorScript1.txt</LastGenOutput>
</None>
I need to remove the TextTemplatingFileGenerator attribute from this tag.
Right-click on the project (tagged as unavailable in solution explorer) and click "Edit yourproj. csproj". This will open up your CSPROJ file for editing. After making the changes you want, save, and close the file.
How to open a CSPROJ file. CSPROJ files are are meant to be opened and edited in Microsoft Visual Studio (Windows, Mac) as part of Visual Studio projects. However, because CSPROJ files are XML files, you can open and edit them in any text or source code editor.
You should be able to do this by right clicking the project in the Solution window and selecting Tools - Edit File. That will open the project file (. csproj) in the text editor.
What is a CSProj file? Files with CSPROJ extension represent a C# project file that contains the list of files included in a project along with the references to system assemblies.
I do this kind of thing a lot. I keep around a set of helper functions for manipulating XML files - particular C# project files. Try this out:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With