How to edit .csproj files using PowerShell - Visual Studio 2010

I need help editing a csproj file using PowerShell. I basically need to select node and change 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.

+7
source share
1 answer

I do so much. I support a set of helper functions for processing XML files - individual C # project files. Try it:

  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)
+10
source

All Articles