Skip to content

Instantly share code, notes, and snippets.

@timheuer
Created December 20, 2019 03:06
Show Gist options
  • Save timheuer/59614c644ecbcb5e89f4d011364d1da8 to your computer and use it in GitHub Desktop.
Save timheuer/59614c644ecbcb5e89f4d011364d1da8 to your computer and use it in GitHub Desktop.
<file>
<categories>
<category>cat1</category>
<category>cat2</category>
<category>removecategory</category>
<category>keepthisone</category>
<category>deletethisone</category>
<category>somethingelse</category>
</categories>
<otherstuff></otherstuff>
</file>
@celsojr
Copy link

celsojr commented Dec 21, 2019

I'm not so good with Powershell but "it worked on my computer" :)
The node with the text "somethingelse" in it has been deleted.

Get-ChildItem sample.xml | % {
    [Xml]$xml = Get-Content $_.FullName

    $xml | Select-Xml -XPath '//category' | % {
        if ($_.InnerXml -eq 'somethingelse') { 
            $_.ParentNode.RemoveChild($_) 
        }
    }

    # $xml.OuterXml | Out-File $_.FullName
    echo $xml.OuterXml
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment