xml - How to add Propertygroup of csproj? -
we doing delay sign of our csproject. opening each csproject file doing time consuming task decided write powershell script.
function new-xmlnode { [cmdletbinding()] [outputtype([string])] param ( # webconfig file full path [parameter(mandatory=$true, valuefrompipelinebypropertyname=$true, position=0)] [string]$path, [string] $xpath, [string] $node, [string] $logfile = "$env:temp\newxmlnode.log" ) try { if (-not (test-path -path $path)) { throw [system.io.filenotfoundexception] "$path not found." } else { $xml = new-object -typename xml $xml.load($path) # getting values $items = select-xml -xml $xml -xpath $xpath if ( $items -ne $null) # if xpath valid { foreach ($item in $items) { [system.xml.xmldocumentfragment] $newnode = $xml.createdocumentfragment() $newnode.innerxml = $node $item.node.appendchild($newnode) } } else # if xpath not valid log error { write "$(get-date -format $datetimeformat) ## error ## $($myinvocation.mycommand.name) ## given xpath $xpath xml $path not valid" | out-file $logfile -append } $xml.save($path) } } catch { write $_.exception.errorrecord | out-file -filepath $logfile -append throw "$(get-date -format $datetimeformat) ## error ## $($myinvocation.mycommand.name) ## $_ " } } function set-strongnamesign { param ( $path="m:\myproject\myproject.csproj" ) $ns = @{ dns = "http://schemas.microsoft.com/developer/msbuild/2003" } new-xmlnode -path $path -xpath "/$ns:project" -node '<propertygroup> <signassembly>true</signassembly> </propertygroup>' } set-strongnamesign
when running getting following error.
## error ## new-xmlnode ## exception calling "appendchild" "1" argument(s): "this document has 'documentelement' node." @ line:51 char:13 + throw "$(get-date -format $datetimeformat) ## error ## $($myinvocati ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : operationstopped: (02/26/2016 11:1...lement' node." :string) [], runtimeexception + fullyqualifiederrorid : 02/26/2016 11:17:33 ## error ## new-xmlnode ## exception calling "appendchild" "1" argument(s): "this document has 'documentelement' node."
how solve this? need add few more propertygroup , 1 itemgroup too.
try add namespace-prefix mapping parameter of function, $namespaces
, , use registered prefix in xpath :
function new-xmlnode { ..... # getting values $items = select-xml -xml $xml -xpath $xpath -namespace $namespaces ...... } function set-strongnamesign { param ( $path="m:\myproject\myproject.csproj" ) $ns = @{ dns = "http://schemas.microsoft.com/developer/msbuild/2003" } # use registered prefix `dns` in xpath , pass mapping parameter : new-xmlnode -path $path -xpath "/dns:project" -namespaces $ns -node '<propertygroup> <signassembly>true</signassembly> </propertygroup>' }
Comments
Post a Comment