我想寻求一些帮助,因为我完全迷失了。
我想检查 .csproj 文件特定部分中的节点是否包含正确的数据。在下面的 xml 片段中,我想取回 PropertyGroup 下属于“Debug|x64”配置文件的“title”的值。
csproj 文件片段
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
...
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<!-- nuget stuff -->
<title>Pet Project</title>
</PropertyGroup>
这是我的 powershell 代码:
function GetConfigPlatformNodeFromProjectFile($projectFile, $nodeIdentifier) {
[xml] $pFile = Get-Content $projectFile
$ns = New-Object System.Xml.XmlNamespaceManager -ArgumentList $pFile.NameTable
$ns.AddNamespace("ns", $pFile.Project.GetAttribute("xmlns"))
$nodes = $pFile.SelectNodes("//ns:Project/PropertyGroup[contains(@Condition,'Debug|x64')]", $ns)
foreach($node in $nodes) {
write-Host "node... " $node
}
}
问题是 $nodes 将始终为 0。根据此处的文章,它应该包含更多内容。路径没问题。我已经检查过很多次了。 xmlns 属性正确返回。我认为问题在于 xpath 本身和命名空间,但是我通过其他 XPath 工具检查了很多次。
我不知道在这种情况下我做错了什么。
提前感谢任何帮助!
András
一定要用xpath吗?否则我会建议这样的事情:
$file = [xml](gc .\test.csproj)
$file.Project.PropertyGroup | ? Condition -Like '*Debug|x64*' | select Title
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)