我的XML数据类似于:
<objects>
<object>
<property name="a" />
<property name="b" />
<property name="hasErrors" />
<property name="data" />
<property/>
<property/>
</property>
</object>
</objects>
在PowerShell中,如何删除具有属性name
特定值的节点?更具体地说,我想删除名称为“hasErrors”和“data”的节点及其子节点(如果有)。
答案 0 :(得分:2)
这段代码应该对您有所帮助,我使用Xpath来匹配条件:
[xml]$data = @"
<objects>
<object>
<property name="a" />
<property name="b" />
<property name="hasErrors" />
<property name="data" />
<property/>
<property/>
</object>
</objects>
"@
$data.SelectNodes("//property[@name = 'data' or @name = 'hasErrors']") | % {$_.ParentNode.removechild($_) } | Out-Null
#to view properties,the empty nodes aren't visible as no value is inside.You can confirm them with count or after saving
$data.objects.object.property
$data.Save("C:\yourpath\mydata.xml")