使用Powershell的.csproj文件中特定节点中的节点列表 [英] List of nodes in a particular node in .csproj file with Powershell

查看:96
本文介绍了使用Powershell的.csproj文件中特定节点中的节点列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想寻求帮助,因为我完全迷路了。

I would like to ask some help because I'm totally lost.

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

I would like to check whether nodes in a particular part of the .csproj files contains proper data or not. In the xml snippet below I would like to get back the value of the "title" under the PropertyGroup belongs to "Debug|x64" profile.

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代码:

Here is my powershell code:

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工具对其进行了多次检查。

Problem is that the $nodes will be always 0. According to the articles here it should contains something more. The path is ok. I've checked many times. The xmlns attribute comes back properly. I think the problem is the xpath itself and the namespace, however I checked it many times by other XPath tools.

我不知道我在做什么错

预先感谢您的帮助!

安德拉斯

推荐答案

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

Do you have to use xpath? Otherwise I would suggest something like this:

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

$file.Project.PropertyGroup | ? Condition -Like '*Debug|x64*' | select Title

这篇关于使用Powershell的.csproj文件中特定节点中的节点列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆