Select-Xml returns file name

363 Views Asked by At

When parsing my *.csproj using Select-Xml, besides from the expected output i also get a concat of a parameter.

What is going on here?

$version = Select-Xml -Path $projectFilePath -XPath "/Project/PropertyGroup/Version"
Write-Host $version
Start-Sleep -Seconds 5
EXIT

this returns

1.0.1:D:\somepath\someproject.csproj

instead of

1.0.1

snippet of csproj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard1.3</TargetFramework>
    </PropertyGroup>
    <PropertyGroup>
        <GeneratePackageOnBuild>false</GeneratePackageOnBuild>
        <AssemblyVersion>1.0.0.0</AssemblyVersion>
        <Version>1.0.1</Version>
    </PropertyGroup>
</Project>
2

There are 2 best solutions below

2
On BEST ANSWER

Thanks for the snippet so now I could try myself.

This does work:

$version = Get-Content -Path $projectFilePath -Raw | Select-Xml -XPath "/Project/PropertyGroup/Version"
Write-Host $version
0
On

you could do this as well:

[xml]$x = get-content D:\somepath\someproject.csproj 
$x.project.propertygroup.version