PowerShell XML SelectNodes fails to process XPath

后端 未结 1 411
难免孤独
难免孤独 2021-02-07 06:23

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.c         


        
1条回答
  •  一整个雨季
    2021-02-07 06:36

    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)

    0 讨论(0)
提交回复
热议问题