从xml文件中删除与列表中的行匹配的行

时间:2019-03-28 18:10:28

标签: powershell

我似乎无法弄清楚如何遍历xml文件并删除与列表中的行匹配的整个“ <option></option>”块。我知道我需要遍历xml文件,然后遍历列表。最后,我将用“过滤结果”替换xml的内容。

XML文件格式:

<comboOptions>
    <option value="1" text="MAINTYPE 1: SOME UNIQUE DESCRIPTION">
        <option value="NOT_REQ" text="SUBTYPE NOT REQUIRED (NOT_REQ)"/>
    </option>
    <option value="13" text="MAINTYPE 2: SOME UNIQUE DESCRIPTION">
        <option value="VALUE1" text="SUBTYPE 1: SOME UNIQUE DESCRIPTION"/>
        <option value="VALUE2" text="SUBTYPE 2: SOME UNIQUE DESCRIPTION"/>
    </option>
    <option value="11" text="MAINTYPE 5: SOME UNIQUE DESCRIPTION">
        <option value="VALUE1" text="SUBTYPE 1: SOME UNIQUE DESCRIPTION"/>
        <option value="VALUE2" text="SUBTYPE 2: SOME UNIQUE DESCRIPTION"/>
        <option value="VALUE3" text="SUBTYPE 3: SOME UNIQUE DESCRIPTION"/>
    </option>
<comboOptions>

到目前为止我的尝试:

[xml]$XmlFIle = Get-Content C:\Path\to\file.xml

$RemoveList = @("MAINTYPE 1: SOME UNIQUE DESCRIPTION", "MAINTYPE 5: SOME UNIQUE DESCRIPTION")

ForEach ($option in $XmlFIle.comboOptions.option)
{
  $RemoveList | Where-Object{ $_ -notcontains $option.text }
}

$XmlFIle.comboOptions.option | Foreach-Object { 
  $myobj1 = $_.text
  $RemoveList | Where-Object { $_ -notcontains $myobj1 }
}

0 个答案:

没有答案