Removing elements and attributes

Elements may be removed from a document by selecting the node, then calling the RemoveChild method on the parent:

[Xml]$xml = @" 
<?xml version="1.0"?> 
<list type='numbers'> 
$node = $xml.SelectSingleNode('/list/*[.="3"]') 
$null = $node.ParentNode.RemoveChild($node) 

The RemoveAll method is also available; however, this removes all children (and attributes) of the selected node.

Attributes are similarly easy to remove from a document:


Get Mastering Windows PowerShell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.