I have the following PowerShell function:
function getProjReferences([string] $projFile) { # Returns nothing $Namespace = @{ ns = "http://schemas.microsoft.com/developer/msbuild/2003"; }; $Xml = Select-Xml -Path $projFile -Namespace $Namespace -XPath "//ns:Project/ItemGroup/Reference" # Returns nothing [xml] $xml = Get-Content -Path $projFile [System.Xml.XmlNamespaceManager] $nsMgr = New-Object -TypeName System.Xml.XmlNamespaceManager($xml.NameTable) $nsMgr.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003"); [XmlNode] $nodes = $xml.SelectNodes("/ns:Project/ItemGroup/Reference", $nsMgr); }
Both attempts return nothing, although the XPath query works fine, I tried without the default namespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003" in xml and its performance.
I understand that I need to map the default namespace to a URI and use it to query the XML with this mapping, but I cannot get it to work.
How can I request XML with a default namespace? I have not yet been able to find anything that can be used on Google.
Update
Work code:
function getProjReferences([string] $projFile) { [xml] $xml = Get-Content -Path $projFile [System.Xml.XmlNamespaceManager] $nsMgr = New-Object -TypeName System.Xml.XmlNamespaceManager($xml.NameTable) $nsMgr.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003"); [XmlNode] $nodes = $xml.SelectNodes("/ns:Project/ns:ItemGroup/ns:Reference", $nsMgr); }
source share