2017-08-02 58 views
0

我有类似XML数据:删除节点匹配特定属性值的

<objects> 
    <object> 
    <property name="a" /> 
    <property name="b" /> 
    <property name="hasErrors" /> 
    <property name="data" /> 
     <property/> 
     <property/> 
    </property> 
    </object> 
</objects> 

在PowerShell中,我将如何删除有属性name特定值的节点?更具体地说,我想删除名称为“hasErrors”和“data”的节点及其子节点(如果有的话)。

回答

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") 
相关问题