Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell XML SelectNodes fails to process XPath

I want to get a list of all project references in my csproj file using PowerShell. Currently I've the following approach:

[xml]$csproj = Get-Content MyProject.csproj

$refs = $csproj.SelectNodes("//ProjectReference")
foreach($ref in $refs) {
  # Later on output more useful information
  Write-Host $ref.Name
}

However, the script does not output anything, although there certainly are ProjectReference elements in the given csproj file. The following is working:

[xml]$csproj = Get-Content MyProject.csproj
foreach($l in $csproj.Project.ItemGroup.ProjectReference) { Write-Host $l.Include }

But I need XPath later on as well + it outputs errors for each ItemGroup which does not contain a ProjectReference - so how to make XPath work using the SelectNodes function?

Sample XML (essentially any VS csproj file with project references will do):

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup></ItemGroup>
  <ItemGroup>
     <ProjectReference Include="Text"></ProjectReference>
     <ProjectReference Include="Text2"></ProjectReference>
  </ItemGroup>
  <ItemGroup></ItemGroup>
</Project>
like image 808
D.R. Avatar asked Apr 15 '14 12:04

D.R.


1 Answers

The problem is the http://schemas.microsoft.com/developer/msbuild/2003 namespace. You need to take account of this namespace in your XPath expressions, as unprefixed element names in XPath refer to elements that are not in a namespace.

[xml]$csproj = Get-Content MyProject.csproj
$ns = new-object Xml.XmlNamespaceManager $csproj.NameTable
$ns.AddNamespace("msb", "http://schemas.microsoft.com/developer/msbuild/2003")

$refs = $csproj.SelectNodes("//msb:ProjectReference", $ns)
foreach($ref in $refs) {
  # Later on output more useful information
  Write-Host $ref.Name
}

(adapted from this answer)

like image 112
Ian Roberts Avatar answered Nov 12 '22 10:11

Ian Roberts