在 Powershell 中解析 XML:转义 XPath 中的各种符号

问题描述 投票:0回答:1

我需要解析 VS2022 .vbproj 文件以查找构建配置的详细信息。 在 powershell 中,我加载文件并尝试访问必要的节点。但我陷入了误导性的 PS 错误报告和大量需要转义的特殊符号......

我正在寻找 .vbproj 中的那一行:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

这是我的脚本:

$xml = [xml](Get-Content $xmlFile)

$propertyGroup = $xml.SelectSingleNode("//PropertyGroup[@Condition= ""''`$(Configuration)|`$(Platform)'' == ''Debug|AnyCPU''""]")
if ($propertyGroup) {
    Write-Host 'Element found.'
} else {
    Write-Host 'Element not found.'
}

无论我做什么,我要么遇到语法错误,要么出现“找不到元素”...请帮忙!

xml powershell visual-studio xpath
1个回答
0
投票

用途:

$xml.SelectSingleNode("//PropertyGroup[normalize-space(translate(@Condition,`"'$`",`"`"))='(Configuration)|(Platform) == Debug|AnyCPU']")

我们转义“和$字符并使用

translate
函数只执行一次。

另一个带有

contains
且不转义的选项 :

$xml.SelectSingleNode("//PropertyGroup[contains(@Condition,'(Configuration)') and contains(@Condition,'(Platform)') and contains(@Condition,'Debug') and contains(@Condition,'AnyCPU')]")
© www.soinside.com 2019 - 2024. All rights reserved.