英文:
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
<?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.
答案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]@"
<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)
答案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 = "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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论