简体   繁体   中英

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?

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.

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 eg XPath, but I haven't dug much into that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM