0

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?

2
  • Parent -> ParentNode Commented Jul 6, 2015 at 10:48
  • RemoveChilde should be RemoveChild perhaps? Commented Jul 6, 2015 at 13:24

1 Answer 1

1

It should be $node.ParentNode.RemoveChild($node)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.