且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 PowerShell 删除 XML 节点?

更新时间:2023-11-24 14:41:10

第二次编辑

如果我没猜错,您想从输入文档中删除 offendingnode.如果您想使用 Select-Xml,您可以这样做:

If I'm not wrong you want to remove the offendingnode from your input document. This what you can do if you want to use Select-Xml:

$offendingnode = select-xml -xpath "/DocIcons/ByExtension/Mapping[@Key='pdf']"  -xml $xmldoc
$xmldoc | select-xml -xpath "/DocIcons/ByExtension" | % {$_.node.removechild($offendingnode.node)}

第一次编辑


1st edit

你要做什么不是很清楚.此外,您正在运行的内容有点混乱.

It's not very clear what you are going to do. Also what you are running is little messed.

使用这一行:

 $xml | Select-Xml -XPath '//Mapping' | ForEach-Object{$_.Node.RemoveAll()}

您将删除 mapping 的所有子项.如果你想删除 ByExtension 下的所有 mapping 你需要像

You are going to remove all children of mapping. If you want to remove all mapping under ByExtension you need something like

 $xml |  Select-Xml -XPath '//ByExtension' | % {$_.Node.RemoveAll()}

如果使用以下方法打印结果会更好:

To print the result is better if you use:

 $xml.outerxml