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 is perfectley valid, I have tried without the default namespace xmlns="http://schemas.microsoft.com/developer/msbuild/2003" in the xml and its working fine.
I understand that I have to map the default namespace to an URI and use this to query the XML with this mapping, but I can't seem to get it to work.
How do I query the XML with a default namespace?, I havent been able to find anything usable on Google yet.
Update
Working 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);
}