List of nodes in a specific node in a .csproj file with Powershell
I would like to ask for some help because I am completely lost.
I would like to check if the nodes in a certain part of the .csproj files contain correct data or not. In the below xml snippet, I would like to return the "title" value in the PropertyGroup relative to the "Debug | x64" profile.
snippet of csproj file
<?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
}
}
The problem is that $ node will always be 0. According to the articles here, it should contain more. The path is fine. I have checked many times. The xmlns attribute is returned correctly. I think the problem is with the xpath and namespace itself, however I have checked it repeatedly with other XPath tools.
I don't know what I am doing wrong in this case.
Thanks for any help in advance!
Andras
+3
source to share