带有Powershell的.csproj文件中特定节点中的节点列表

Say*_*ndo 5 xml powershell xpath csproj

我想请一些帮助,因为我完全迷失了.

我想检查.csproj文件的特定部分中的节点是否包含正确的数据.在下面的xml片段中,我想回到PropertyGroup下的"title"属于"Debug | x64"配置文件的值.

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>
Run Code Online (Sandbox Code Playgroud)

这是我的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
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是$节点将始终为0.根据这里的文章,它应该包含更多内容.路径还可以.我多次检查过.xmlns属性正确返回.我认为问题是xpath本身和命名空间,但是我通过其他XPath工具多次检查它.

我不知道在这种情况下我做错了什么.

提前感谢任何帮助!

安德拉什

Swo*_*kie 10

你必须使用xpath吗?否则我会建议这样的事情:

$file = [xml](gc .\test.csproj)

$file.Project.PropertyGroup | ? Condition -Like '*Debug|x64*' | select Title
Run Code Online (Sandbox Code Playgroud)

  • 我收到一个错误:输入名称"Condition"无法解析为属性. (2认同)