0

我有一个 Xml 类型的节点

<compilation defaultLanguage="c#" debug="true" targetframework="4.0">     
    </compilation> 

我想在 powershell 中删除属性调试和目标框架

<compilation defaultLanguage="c#">     
        </compilation> 

我正在使用以下代码这样做

function Remove-Attribute([xml]$document,[string]$propertyName)
{
    try
    {   
        $ns = New-Object Xml.XmlNamespaceManager $document.NameTable
        $ns.AddNamespace("ns","WDA.Application.Configuration")
        $configurationInfo = Get-ConfigurationInfo $propertyName

        $node = $document.selectSingleNode($ConfigurationInfo.XPath,$ns)
        If($node -eq $null)
        {
             throw "ERROR: The '" + $propertyName + "' setting was not found using XPath: " + $configurationInfo.XPath
         }
        else
         {    
        $node.Attributes("debug").Remove()
            $node.Attributes("targetframework").Remove()

        $document.Save()
        }
    }
    catch [Exception]
    {
    }
}

但我无法删除节点。请帮助:)

4

2 回答 2

1

尝试使用RemoveAttribute(). 这是一个例子:

$xml = @'
<xml>
<compilation defaultLanguage="c#" debug="true" targetframework="4.0"></compilation>
</xml>
'@ -as [xml]

$node = $xml.selectSingleNode('//compilation')
$node.RemoveAttribute('debug')
$node.RemoveAttribute('targetframework')

$xml.OuterXml
于 2013-05-29T13:13:13.117 回答
0

这工作正常:

$xml.root.compilation

defaultLanguage                   debug                            targetframework
---------------                   -----                            ---------------
c#                                true                             4.0

$xml.root.compilation.RemoveAttribute("debug")
$xml.root.compilation.RemoveAttribute("targetframework")

$xml.root.compilation

defaultLanguage
---------------
c# 
于 2013-05-29T13:16:21.313 回答