6

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

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 file snippet

<?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>

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
    }
}

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.

I don't know what I'm doing wrong in this case.

Thanks any help in advance!

András

AndrasCsanyi
  • 3,943
  • 8
  • 45
  • 77

3 Answers3

12

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
  • It seems very good! Thanks! On the other hand, I don't have to use xpath, just I have a lot of experience with it due to I'm a test engineer. I'll take a try and your answer will be the accepted. – AndrasCsanyi Nov 07 '14 at 12:48
  • 2
    I get an error: The input name "Condition" cannot be resolved to a property. –  Jul 06 '17 at 04:06
  • You get that error because you use PowerShell 2, which doesn't support this syntax. Write it like this instead: `$file.Project.PropertyGroup | ? { $_.Condition -Like '*Debug|x64*' } | select Title` –  Dec 05 '18 at 16:39
0

I solved mine this way:

[xml]$Project = Get-Content -Path $Path
$ns = New-Object System.Xml.XmlNamespaceManager -ArgumentList $Project.NameTable
$ns.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003")
$Config = $Project.SelectSingleNode("//ns:Project/ns:PropertyGroup[contains(@Condition, 'Release')]", $ns)
if (-not $Config) {
    throw "Could not find configuration"
}
crh225
  • 821
  • 18
  • 34
0

My requirement was to extract project build framework from cproj file. But my code allows to extract more data. You just need to modify it by your needs.

function GetProjectFrameworks([string]$projectName)
{
    Write-Host "GetProjectFrameworks Read Start---------------" 

    $xml = [Xml](Get-Content "$projectName.csproj")
    $frameworkValue  = ""
    [bool] $frameworkjobDone = $false

    foreach($item in $xml.ChildNodes)
    {
        if($frameworkjobDone) { break } 

        foreach($node in $item.ChildNodes)
        {
            if($frameworkjobDone) { break } 

            foreach($obj in $node.ChildNodes)
            {
                if($obj.Name -Match 'TargetFramework')
                {
                    $frameworkValue = $obj.InnerXml 
                    $frameworkjobDone = $true
                    break 
                }
            }
        } 
    }

    $FrameworkArray = ($frameworkValue -split ';')
    # foreach($fram in $FrameworkArray)
    # {
    #     Write-Host $fram    
    # }
    Write-Host "GetProjectFrameworks Read Finish---------------" 
    $FrameworkArray
}

Hope this will help somebody.

Jevgenij Kononov
  • 1,210
  • 16
  • 11