PowerShell XML SelectNodes fails to process XPath

2019-03-18 08:29发布

问题:

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>

回答1:

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)