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'> 
<name>1</name> 
<name>2</name> 
<name>3</name> 
</list> 
"@ 
$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:

$xml.list.RemoveAttribute('type') 

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.