簡體   English   中英

Selecet XML節點在Powershell中的名稱相似

[英]Selecet XML Nodes by similar names in Powershell

我有一個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>

我想選擇帶有Name Software*所有節點來刪除該值。

只需使用Get-Content cmdlet加載文件,然后將結果轉換為xml 然后使用Where-Object cmdlet過濾ImageConfig.Image所有子項,其中名稱與Software\\d+匹配,並刪除該值。 最后,使用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