Jarno343
Jarno343

Reputation: 91

Powershell remove XML tags

I’m new to PowerShell and am trying to remove a XML element from a xml XML file of mine.

To do this I have used the following example code that got from Adding and Removing Xmlnode using Powershell

The example works well but when I try to adept it for my code it no longer works. The code is as follows:

$Output = "C:\Users\Desktop\resulttest.xml"
# Load the existing document
$Doc = [xml](Get-Content -Path C:\Users\Desktop\test.xml)

# Specify tag names to delete and then find them
$DeleteNames= "body"
($Doc.Task.ChildNodes | Where-Object { $DeleteNames -contains $_.Name }) | ForEach-Object {
    # Remove each node from its parent
    [void]$_.ParentNode.RemoveChild($_)
}
# Save the modified document
$Doc.Save($Output)

I use the following example XML file:

<html>a
<body>b</body>
<Price>300</Price>
<Total> 5000 </Total>
 <Part_C>
 <Name>Jar</Name>
<Title>Mr</Title>
<CountryCode> 5000 </CountryCode>
</Part_C>
</html>

The goal of the XML file is that it removes multiple tags like "Price" and "Total" or "Price", "Title", "Total"

But when I run the code it still contains these tags .

My question is what I am doing wrong and how can I ensure that PowerShell removes the XML tags?

Upvotes: 1

Views: 3461

Answers (2)

Torbj&#246;rn Bergstedt
Torbj&#246;rn Bergstedt

Reputation: 3429

Without going too deep into it, I think the problem may be that you're not operating on the $Doc element itself. I'd use XML methods SelectNodes directly on it:

$Doc.ChildNodes.SelectNodes($DeleteNames) | Foreach-Object {
    $Doc.ChildNodes.RemoveChild($_)
}
$Doc.Save($Output)

UPDATE: If $DeleteNames contain more than one criteria/node, one would need to make it an array and call the code once for each (SelectNodes does not support patterns):

,"Price","Total" | Foreach-Object { 
    $Doc.ChildNodes.SelectNodes($_) | Foreach-Object {
...

I suppose you could do an alternate node selection using e.g. XPath, but I haven't dug much into that.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338208

This works perfectly.

$Output = "resulttest.xml"
$Doc = [xml]"<task><body>b</body><bla>bla</bla></task>"

$DeleteNames = "body"

$Doc.Task.ChildNodes | Where-Object { $DeleteNames -eq $_.Name } | ForEach-Object {
    $_.ParentNode.RemoveChild($_) | Out-Null
}

$Doc.Save($Output)

My guess would be that $Doc.Task does not actually select anything in your XML document.

Upvotes: 2

Related Questions