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

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

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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;FilingJob xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  3. &lt;Batch SeqNum=&quot;1&quot;&gt;
  4. &lt;FileRoom&gt;Test&lt;/FileRoom&gt;
  5. &lt;DeleteFiles&gt;True&lt;/DeleteFiles&gt;
  6. &lt;Document SeqNum=&quot;1&quot;&gt;
  7. &lt;UserDocNum&gt;1&lt;/UserDocNum&gt;
  8. &lt;DocName&gt;CARD DISPUTE PACKET-DEBIT&lt;/DocName&gt;
  9. &lt;Cabinet&gt;MEMBER DISPUTES&lt;/Cabinet&gt;
  10. &lt;Type&gt;CARD DISPUTE PACKET-DEBIT&lt;/Type&gt;
  11. &lt;Institution&gt;149&lt;/Institution&gt;
  12. &lt;Indexes&gt;
  13. &lt;Index Name=&quot;DOC DATE&quot;&gt;08/03/2023&lt;/Index&gt;
  14. &lt;Index Name=&quot;NAME&quot;&gt;Test Test&lt;/Index&gt;
  15. &lt;Index Name=&quot;CIF&quot;&gt;123424446&lt;/Index&gt;
  16. &lt;Index Name=&quot;BRANCH&quot;&gt;2&lt;/Index&gt;
  17. &lt;/Indexes&gt;
  18. &lt;Pages&gt;
  19. &lt;Page SeqNum=&quot;1&quot;&gt;253_ATM_-_Unauthorized_CHK_-80.PDF&lt;/Page&gt;
  20. &lt;/Pages&gt;
  21. &lt;/Document&gt;
  22. &lt;/Batch&gt;
  23. &lt;Batch SeqNum=&quot;2&quot;&gt;
  24. &lt;FileRoom&gt;Test&lt;/FileRoom&gt;
  25. &lt;DeleteFiles&gt;True&lt;/DeleteFiles&gt;
  26. &lt;Document SeqNum=&quot;2&quot;&gt;
  27. &lt;UserDocNum&gt;2&lt;/UserDocNum&gt;
  28. &lt;DocName&gt;CARD DISPUTE PACKET-DEBIT&lt;/DocName&gt;
  29. &lt;Cabinet&gt;MEMBER DISPUTES&lt;/Cabinet&gt;
  30. &lt;Type&gt;CARD DISPUTE PACKET-DEBIT&lt;/Type&gt;
  31. &lt;Institution&gt;149&lt;/Institution&gt;
  32. &lt;Indexes&gt;
  33. &lt;Index Name=&quot;DOC DATE&quot;&gt;08/03/2023&lt;/Index&gt;
  34. &lt;Index Name=&quot;CIF&quot;&gt;54353545346&lt;/Index&gt;
  35. &lt;Index Name=&quot;BRANCH&quot;&gt;2&lt;/Index&gt;
  36. &lt;/Indexes&gt;
  37. &lt;Pages&gt;
  38. &lt;Page SeqNum=&quot;2&quot;&gt;253_ATM_-_Unauthorized_CHK_-80.PDF&lt;/Page&gt;
  39. &lt;/Pages&gt;
  40. &lt;/Document&gt;
  41. &lt;/Batch&gt;
  42. &lt;Batch SeqNum=&quot;3&quot;&gt;
  43. &lt;FileRoom&gt;Test&lt;/FileRoom&gt;
  44. &lt;DeleteFiles&gt;True&lt;/DeleteFiles&gt;
  45. &lt;Document SeqNum=&quot;3&quot;&gt;
  46. &lt;UserDocNum&gt;3&lt;/UserDocNum&gt;
  47. &lt;DocName&gt;CARD DISPUTE PACKET-DEBIT&lt;/DocName&gt;
  48. &lt;Cabinet&gt;MEMBER DISPUTES&lt;/Cabinet&gt;
  49. &lt;Type&gt;CARD DISPUTE PACKET-DEBIT&lt;/Type&gt;
  50. &lt;Institution&gt;149&lt;/Institution&gt;
  51. &lt;Indexes&gt;
  52. &lt;Index Name=&quot;DOC DATE&quot;&gt;08/03/2023&lt;/Index&gt;
  53. &lt;Index Name=&quot;CIF&quot;&gt;20141960000005821&lt;/Index&gt;
  54. &lt;Index Name=&quot;BRANCH&quot;&gt;2&lt;/Index&gt;
  55. &lt;/Indexes&gt;
  56. &lt;Pages&gt;
  57. &lt;Page SeqNum=&quot;3&quot;&gt;253_ATM_-_Unauthorized_CHK_-80.PDF&lt;/Page&gt;
  58. &lt;/Pages&gt;
  59. &lt;/Document&gt;
  60. &lt;/Batch&gt;
  61. &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

  1. 它可能是如此简单:
  2. $xml = [Xml]@"
  3. <Batch SeqNum="1">
  4. <FileRoom>Test</FileRoom>
  5. <DeleteFiles>True</DeleteFiles>
  6. <Document SeqNum="1">
  7. <Indexes>
  8. <Index Name="DOC DATE">08/03/2023</Index>
  9. <Index Name="CIF">123456789</Index>
  10. <Index Name="BRANCH">2</Index>
  11. </Indexes>
  12. </Document>
  13. </Batch>
  14. "@
  15. $node = $xml.SelectSingleNode('//Index[@Name="CIF"]')
  16. $dest = $node.ParentNode
  17. $dest.RemoveChild($node)
英文:

It could be something as simple as

  1. $xml=[Xml]@&quot;
  2. &lt;Batch SeqNum=&quot;1&quot;&gt;
  3. &lt;FileRoom&gt;Test&lt;/FileRoom&gt;
  4. &lt;DeleteFiles&gt;True&lt;/DeleteFiles&gt;
  5. &lt;Document SeqNum=&quot;1&quot;&gt;
  6. &lt;Indexes&gt;
  7. &lt;Index Name=&quot;DOC DATE&quot;&gt;08/03/2023&lt;/Index&gt;
  8. &lt;Index Name=&quot;CIF&quot;&gt;123456789&lt;/Index&gt;
  9. &lt;Index Name=&quot;BRANCH&quot;&gt;2&lt;/Index&gt;
  10. &lt;/Indexes&gt;
  11. &lt;/Document&gt;
  12. &lt;/Batch&gt;
  13. &quot;@
  14. $node = $xml.SelectSingleNode(&#39;//Index[@Name=&quot;CIF&quot;]&#39;)
  15. $dest = $node.ParentNode
  16. $dest.RemoveChild($node)

答案2

得分: 0

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

  1. $file = "C:\XXX\XXXXXXXXX\Test.xml"
  2. [xml]$xml = Get-Content $file
  3. $Xml.SelectNodes("//Index[@Name='CIF']") | ForEach-Object {
  4. $_.ParentNode.RemoveChild($_) | Out-Null
  5. } -End {
  6. $xml.Save($file)
  7. }
英文:

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

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

  1. $file = &quot;C:\XXX\XXXXXXXXX\Test.xml&quot;
  2. [xml]$xml = Get-Content $file
  3. $Xml.SelectNodes(&quot;//Index[@Name=&#39;CIF&#39;]&quot;) | ForEach-Object {
  4. $_.ParentNode.RemoveChild($_) | Out-Null
  5. } -End {
  6. $xml.Save($file)
  7. }

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:

确定