I've got an xml file, containing several book names:
<?xml version="1.0" encoding="utf-8"?>
<Project>
<ItemGroup>
<Book Name="Learn Powershell" />
<Book Name=".net Programming" />
<Book Name="C# Programming" />
</ItemGroup>
</Project>
Then I've got a powershell script, trying to find out book name that containing "powershell" and remove it from xml:
[xml]$xml=Get-Content "D:\m.xml"
$p = $xml.Project.ItemGroup.Book
$node=$p|?{$_.Name.Contains("Powershell")}
$node.Parent.RemoveChilde($node)
It reports runtime exception:
PS D:\> D:\Untitled4.ps1
You cannot call a method on a null-valued expression.
At D:\Untitled4.ps1:4 char:1
+ $node.Parent.RemoveChild($node)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Why? How to correct my code?
Parent->ParentNodeRemoveChildeshould beRemoveChildperhaps?