简体   繁体   中英

How to add Propertygroup of csproj?

We are doing delay sign for all of our csproject. Opening each csproject file for doing this is time consuming task so I decided to write a 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 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

When running this I am getting following error.

## 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."

How to solve this? I need to add few more propertygroup and one Itemgroup too.

Try to add the namespace-prefix mapping as another parameter of your function, say $namespaces , and use the registered prefix in your XPath :

function New-XMLNode
{
    .....
    # Getting all the 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 the registered prefix `dns` in the XPath and pass the mapping as parameter :
    New-XMLNode -Path $Path -xPath "/dns:Project" -namespaces $ns -node '<PropertyGroup>
    <SignAssembly>true</SignAssembly>
    </PropertyGroup>'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM