Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How do I remove multiple XML elements?

$
0
0

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?


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>