0

I have this XML file. Path is C:Temp\Test.xml

<?xml version="1.0" encoding="utf-8"?>
<FilingJob xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Batch SeqNum="1">
    <FileRoom>Test</FileRoom>
    <DeleteFiles>True</DeleteFiles>
    <Document SeqNum="1">
      <UserDocNum>1</UserDocNum>
      <DocName>CARD DISPUTE PACKET-DEBIT</DocName>
      <Cabinet>MEMBER DISPUTES</Cabinet>
      <Type>CARD DISPUTE PACKET-DEBIT</Type>
      <Institution>149</Institution>
      <Indexes>
        <Index Name="DOC DATE">08/03/2023</Index>
        <Index Name="NAME">Test Test</Index>
        <Index Name="CIF">123424446</Index>
        <Index Name="BRANCH">2</Index>
      </Indexes>
      <Pages>
        <Page SeqNum="1">253_ATM_-_Unauthorized_CHK_-80.PDF</Page>
      </Pages>
    </Document>
  </Batch>
    <Batch SeqNum="2">
    <FileRoom>Test</FileRoom>
    <DeleteFiles>True</DeleteFiles>
    <Document SeqNum="2">
      <UserDocNum>2</UserDocNum>
      <DocName>CARD DISPUTE PACKET-DEBIT</DocName>
      <Cabinet>MEMBER DISPUTES</Cabinet>
      <Type>CARD DISPUTE PACKET-DEBIT</Type>
      <Institution>149</Institution>
      <Indexes>
        <Index Name="DOC DATE">08/03/2023</Index>
        <Index Name="CIF">54353545346</Index>
        <Index Name="BRANCH">2</Index>
      </Indexes>
      <Pages>
        <Page SeqNum="2">253_ATM_-_Unauthorized_CHK_-80.PDF</Page>
      </Pages>
    </Document>
  </Batch>
    <Batch SeqNum="3">
    <FileRoom>Test</FileRoom>
    <DeleteFiles>True</DeleteFiles>
    <Document SeqNum="3">
      <UserDocNum>3</UserDocNum>
      <DocName>CARD DISPUTE PACKET-DEBIT</DocName>
      <Cabinet>MEMBER DISPUTES</Cabinet>
      <Type>CARD DISPUTE PACKET-DEBIT</Type>
      <Institution>149</Institution>
      <Indexes>
        <Index Name="DOC DATE">08/03/2023</Index>
        <Index Name="CIF">20141960000005821</Index>
        <Index Name="BRANCH">2</Index>
      </Indexes>
      <Pages>
        <Page SeqNum="3">253_ATM_-_Unauthorized_CHK_-80.PDF</Page>
      </Pages>
    </Document>
  </Batch>
</FilingJob>

I want to remove the whole line that has the "Index Name"= CIF That number between the >123456789< is never going to be the same number. How do I get this to work in PowerShell?

Found a Few Remove Child Items but can't seem to get it to work.

mhughey
  • 31
  • 4

2 Answers2

3

It could be something as simple as

$xml=[Xml]@"
<Batch SeqNum="1">
   <FileRoom>Test</FileRoom>
   <DeleteFiles>True</DeleteFiles>
   <Document SeqNum="1">
      <Indexes>
         <Index Name="DOC DATE">08/03/2023</Index>
         <Index Name="CIF">123456789</Index>
         <Index Name="BRANCH">2</Index>
      </Indexes>
   </Document>
</Batch>

"@

$node = $xml.SelectSingleNode('//Index[@Name="CIF"]') 
$dest = $node.ParentNode
$dest.RemoveChild($node)
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
0

I got it. This is what I did. Thank you both for your help!!!

$file = "C:\XXX\XXXXXXXXX\Test.xml"

[xml]$xml = Get-Content $file
$Xml.SelectNodes("//Index[@Name='CIF']") | ForEach-Object {
  $_.ParentNode.RemoveChild($_) | Out-Null
} -End {
  $xml.Save($file)
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
mhughey
  • 31
  • 4
  • 2
    Nice, though given that Jack Fleeting's answer provided the gist of the solution, it would have been better to work with him to generalize his answer - especially since you didn't specify the requirement to process _all_ matching nodes up front. As a general tip: you can help future readers by clearly signaling which answer, if any, solved your problem, namely by [accepting](https://meta.stackexchange.com/a/5235/248777) it. – mklement0 Aug 04 '23 at 20:54
  • 2
    As an aside: `[xml] (Get-Content ...)` should be avoided, because `Get-Content` may misinterpret the character encoding of XML files. Use the `[xml]` type's `.Load()` method instead; e.g.: `($xmlDoc = [xml]::new()).Load((Convert-Path ./file.xml))` - see the bottom section of [this answer](https://stackoverflow.com/a/68087177/45375) for details. If you're confident that `Get-Content` won't misinterpret your XML file, add the `-Raw` switch to speed up the operation. – mklement0 Aug 04 '23 at 20:57