remove element from xml file

Welcome Forums General PowerShell Q&A remove element from xml file

Viewing 6 reply threads
  • Author
    Posts
    • #245775
      Participant
      Topics: 11
      Replies: 15
      Points: 104
      Rank: Participant

      I have xml file which has a multiple element with same name

      for example

      <data>

      <name></name>

      <test>1</test>

      <test>2</test>

      <test>3</test>

      &lt;/data&gt;

      I want to remove the element from xml if it matchs 3 for example.

      I try following but it didnt work

      $file_data.test| ? {$_ -eq 3 } | % {$file_data.test.RemoveChild($_)} | Out-Null

    • #245781
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero
    • #245790
      Senior Moderator
      Topics: 10
      Replies: 168
      Points: 899
      Helping Hand
      Rank: Major Contributor

      Basically, you want to use the Select-Xml cmdlet for this. Have a look at this article: Mastering Everyday XML Tasks in PowerShell. The “Finding Information in XML Files” and “Removing XML Information” sections should give you what you need.

    • #245853
      Participant
      Topics: 8
      Replies: 568
      Points: 2,170
      Helping Hand
      Rank: Community Hero

      You’re approach is looking for the value 3, instead of the test node with a value of 3. Find the node via XPath and then remove it. Here is one way using Grokkit’s suggestion, Select-XML

      And another using SelectSingleNode

      The output for both

    • #246093
      Participant
      Topics: 11
      Replies: 15
      Points: 104
      Rank: Participant

      I try these way but it doesnt work on my file.

      [xml]$file = '&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
      &lt;movie&gt;
      &lt;plot /&gt;
      &lt;outline /&gt;
      &lt;customrating&gt;12&lt;/customrating&gt;
      &lt;lockdata&gt;true&lt;/lockdata&gt;
      &lt;dateadded&gt;2019-04-09 19:32:26&lt;/dateadded&gt;
      &lt;title&gt;Name of the file&lt;/title&gt;
      &lt;rating&gt;4.5&lt;/rating&gt;
      &lt;year&gt;2018&lt;/year&gt;
      &lt;mpaa&gt;15&lt;/mpaa&gt;
      &lt;imdbid&gt;13218067991122&lt;/imdbid&gt;
      &lt;premiered&gt;2018-11-19&lt;/premiered&gt;
      &lt;releasedate&gt;2018-11-19&lt;/releasedate&gt;
      &lt;criticrating&gt;5&lt;/criticrating&gt;
      &lt;runtime&gt;41&lt;/runtime&gt;
      &lt;genre&gt;genre&lt;/genre&gt;
      &lt;genre&gt;genre2&lt;/genre&gt;
      &lt;genre&gt;genre3&lt;/genre&gt;
      &lt;genre&gt;genre4&lt;/genre&gt;
      &lt;genre&gt;genre genre&lt;/genre&gt;
      &lt;genre&gt;genre 5&lt;/genre&gt;
      &lt;genre&gt;genre genre 2&lt;/genre&gt;
      &lt;studio&gt;Studio&lt;/studio&gt;
      &lt;isuserfavorite&gt;false&lt;/isuserfavorite&gt;
      &lt;playcount&gt;0&lt;/playcount&gt;
      &lt;watched&gt;false&lt;/watched&gt;
      &lt;resume&gt;
      &lt;position&gt;0&lt;/position&gt;
      &lt;total&gt;2470.906&lt;/total&gt;
      &lt;/resume&gt;
      &lt;actor&gt;
      &lt;name&gt;Actor 1&lt;/name&gt;
      &lt;type&gt;Actor&lt;/type&gt;
      &lt;/actor&gt;
      &lt;actor&gt;
      &lt;name&gt;Actor 2&lt;/name&gt;
      &lt;type&gt;Actor&lt;/type&gt;
      &lt;/actor&gt;
      &lt;id&gt;13218067991122&lt;/id&gt;
      &lt;/movie&gt;';

       

      $file_data = $file.DocumentElement

      $newarray = @(
      "genre",
      "genre2",
      "genre3",
      "genre4",
      "genre genre 3",
      "genre 5",
      "genre genre 2"
      )

      $oldarray = $file_data.genre

      Compare-Object -IncludeEqual -ReferenceObject $oldArray -DifferenceObject $newarray | Where-Object -FilterScript {

      #if($_.SideIndicator -eq "=&gt;"){Write-Host "New Object - " $_.InputObject}
      if($_.SideIndicator -eq "&lt;="){
      $w=$_.InputObject
      $file_data.SelectSingleNode("//genre['$w']") | foreach {[void]$_.parentnode.removechild($_)}
      #Write-Host "Remove Object - " $_.InputObject
      }

      }
      $file_data.genre

    • #246156
      Participant
      Topics: 8
      Replies: 568
      Points: 2,170
      Helping Hand
      Rank: Community Hero

      It does work in my tests, except it gets rid of the first entry. Your data has spaces in it so try this.

      [/crayon]
    • #246411
      Participant
      Topics: 11
      Replies: 15
      Points: 104
      Rank: Participant

      It does work in my tests, except it gets rid of the first entry. Your data has spaces in it so try this.

      <br />Compare-Object -IncludeEqual -ReferenceObject $oldArray -DifferenceObject $newarray | Where-Object -FilterScript {</p><p>#if($_.SideIndicator -eq "=&gt;"){Write-Host "New Object - " $_.InputObject}<br />if($_.SideIndicator -eq "&lt;="){<br />$w=$_.InputObject<br />$file_data.SelectSingleNode("//genre[text()='$w']") | foreach {[void]$_.parentnode.removechild($_)}<br />#Write-Host "Remove Object - $w"<br />}</p><p>}<br />$file_data.genre

      This work. Thank you

Viewing 6 reply threads
  • You must be logged in to reply to this topic.