如何添加csproj的Propertygroup?

时间:2016-02-26 05:54:21

标签: xml powershell msbuild

我们正在为所有csproject做延迟签名。打开每个csproject文件执行此操作非常耗时,因此我决定编写PowerShell脚本。

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 all the values
                $Items  = Select-Xml -XML $xml -XPath $xPath
                If ( $Items -ne $null) # If xpath is valid 
                {
                    ForEach ($Item in $Items) 
                    {    
                        [System.Xml.XmlDocumentFragment] $newnode = $xml.CreateDocumentFragment()
                        $newnode.InnerXml = $node
                        $Item.Node.AppendChild($newnode)
                    } 
                }
                else # if xpath is not valid then log the error 
                {
                    Write "$(Get-Date -Format $DateTimeFormat) ## ERROR ## $($MyInvocation.MyCommand.Name) ## Given xpath $xpath for xml $Path is 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

运行此操作时,我收到以下错误。

## ERROR ## New-XMLNode ## Exception calling "AppendChild" with "1" argument(s):
"This document already has a 'DocumentElement' node."
At 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" with "1" argument(s): "This document already has a 'DocumentElement' node."

如何解决这个问题?我还需要添加更多的属性组和一个Itemgroup。

1 个答案:

答案 0 :(得分:0)

尝试将namespace-prefix映射添加为函数的另一个参数,例如def f1(x, y, **kwargs): return x + y def f2(z, y, **kwargs): return z - y def fmul(f1, f2): return lambda x, y, z: f1(x=x, y=y, z=z) * f2(x=x, y=y, z=z) ,并在XPath中使用已注册的前缀:

$namespaces