我有一个类型为
的Xml节点<compilation defaultLanguage="c#" debug="true" targetframework="4.0">
</compilation>
我想删除powershell中的属性debug和targetframework
<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]
{
}
}
但是我无法移除节点。请帮助:)
答案 0 :(得分:0)
尝试使用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
答案 1 :(得分: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#