简体   繁体   English

Selecet XML节点在Powershell中的名称相似

[英]Selecet XML Nodes by similar names in Powershell

I have a xml File which looks like: 我有一个xml文件,看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ImageConfig>
  <Image Name="">
    <Include-Software></Include-Software>
    <RoboCopy></RoboCopy>
    <Image-Path></Image-Path>
    <Software0>xyz</Software0>
    <Software1>abc</Software1>
    <Software2>def</Software2>
    <Software3>ghf</Software3>
    <Software4>wew</Software4>
    <Software5>hjf</Software5>
  </Image>
</ImageConfig>

I want select all nodes with the with the Name Software* to delete the value. 我想选择带有Name Software*所有节点来删除该值。

Just load the file using the Get-Content cmdlet and cast the result to xml . 只需使用Get-Content cmdlet加载文件,然后将结果转换为xml Then filter all children of ImageConfig.Image where the name match Software\\d+ using the Where-Object cmdlet and remove the value. 然后使用Where-Object cmdlet过滤ImageConfig.Image所有子项,其中名称与Software\\d+匹配,并删除该值。 Finally, write the xml back using the Save method: 最后,使用Save方法将xml写回:

$xmlFilePath = 'your_path_here.xml'

[xml]$xml  = Get-Content $xmlFilePath
$xml.ImageConfig.Image.ChildNodes | Where-Object Name -Match 'Software\d+' | ForEach-Object {
    $_.'#text' = ''
}

$xml.Save($xmlFilePath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM