How do I remove multiple XML elements?

Hi guys,

I am writing a script that  amends an XML file. I'm still troubleshooting some of the finer points, but to save time, I was thinking I add something to my code the removes the new xml I have added, every time I run the code again.

<employees>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
</employees>


The above is an example of what my script does to the employees.xml. It creates newXML nodes with a text node within them. 

Every time I run my script it just adds more and more nodes to the list. Not what I really want. So is there anyway to get rid of all these newXML nodes?

I've tried this:

try{
    $node = $xdoc."employees"
    
    $i=0

    foreach($item in $node)
    {
        $i= $i+1
        $node.ParentNode.RemoveChild($node[$i])
    }
}
catch{ write-host "The employees node is empty" }

When I run this, it always goes straight to my catch. Any thoughts?

March 25th, 2015 7:55am

You have your terms a bit mixed.  You are creating an element within and element that contains a text node.

You also need to look at how the XML object is structured. You are just playing with text and not XML.

$xml=[xml]@'
<employees>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML>
        <newTextNode>temp</newTextNode>
    </newXML>
</employees>
'@
$xml.employees | %{$_.RemoveAll()}

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 10:06am

I like the above answer, but that would only work if I wanted to remove all elements in the employees. 

I worked out the following solution. 

$node = $doc.SelectSingleNode("//newXML")

while ($node -ne $null)
{
    $node.ParetnNode.RemoveChild($node)
    $node = $doc.SelectSingleNode("//newXML")
} 

March 26th, 2015 7:08am

Unfortunately without an identifier you cannot know which node is being removed.  There is no guaranteed order in XML.

$xml=[xml]@'
<employees>
    <newXML id="3">
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML id="1">
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML id="33">
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML id="4">
        <newTextNode>temp</newTextNode>
    </newXML>
    <newXML id="77">
        <newTextNode>temp</newTextNode>
    </newXML>
</employees>
'@
$xml.SelectSingleNode('//newXML[@id="33"]')|%{$_.RemoveAll()}

Free Windows Admin Tool Kit Click here and download it now
March 26th, 2015 7:49am

I like the above answer, but that would only work if I wanted to remove all elements in the employees. 

I worked out the following solution. 

$node = $doc.SelectSingleNode("//newXML")

while ($node -ne $null)
{
    $node.ParetnNode.RemoveChild($node)
    $node = $doc.SelectSingleNode("//newXML")
} 

  • Marked as answer by KarneeKarnay Thursday, March 26, 2015 11:07 AM
March 26th, 2015 11:07am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics