需要使用PowerShell从XML中移除节点。

huangapple go评论85阅读模式
英文:

Need to remove Node from XML with Powershell

问题

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?

我想删除包含"Index Name"= CIF的整行。<123456789<之间的数字永远不会相同。如何在PowerShell中实现这个目标?

英文:

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

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

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.

答案1

得分: 3

它可能是如此简单:

$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)
英文:

It could be something as simple as

$xml=[Xml]@&quot;
&lt;Batch SeqNum=&quot;1&quot;&gt;
   &lt;FileRoom&gt;Test&lt;/FileRoom&gt;
   &lt;DeleteFiles&gt;True&lt;/DeleteFiles&gt;
   &lt;Document SeqNum=&quot;1&quot;&gt;
      &lt;Indexes&gt;
         &lt;Index Name=&quot;DOC DATE&quot;&gt;08/03/2023&lt;/Index&gt;
         &lt;Index Name=&quot;CIF&quot;&gt;123456789&lt;/Index&gt;
         &lt;Index Name=&quot;BRANCH&quot;&gt;2&lt;/Index&gt;
      &lt;/Indexes&gt;
   &lt;/Document&gt;
&lt;/Batch&gt;

&quot;@

$node = $xml.SelectSingleNode(&#39;//Index[@Name=&quot;CIF&quot;]&#39;) 
$dest = $node.ParentNode
$dest.RemoveChild($node)

答案2

得分: 0

我知道了。这是我所做的。谢谢你们两位的帮助!!!

$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)
}
英文:

<!-- language-all: sh -->

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

$file = &quot;C:\XXX\XXXXXXXXX\Test.xml&quot;

[xml]$xml = Get-Content $file
$Xml.SelectNodes(&quot;//Index[@Name=&#39;CIF&#39;]&quot;) | ForEach-Object {
  $_.ParentNode.RemoveChild($_) | Out-Null
} -End {
  $xml.Save($file)
}

huangapple
  • 本文由 发表于 2023年8月5日 00:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定