2013-05-29 41 views
0

的屬性我有刪除的XElement

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

我想刪除屬性調試和targetframework在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] 
    { 
    } 
} 
類型的XML節點

但我無法刪除節點。請幫助:)

回答

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 
+0

:非常感謝你,它的工作:) – user1595214

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#