PowerShell XML SelectNodes fails to process XPath PowerShell XML SelectNodes fails to process XPath powershell powershell

PowerShell XML SelectNodes fails to process XPath


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)