How do I remove an XML node using PowerShell?

若如初见. 提交于 2019-12-05 13:21:07
Emiliano Poggi

2nd edit

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.

With this line:

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!