Using PowerShell, how do I add multiple namespaces (one of which is the default namespace)?

后端 未结 2 789
慢半拍i
慢半拍i 2020-12-19 01:00

I have an XML document that contains two namespaces (the \'default\' namespace and xlink):

  • xmlns=\"http://embassy/schemas/dudezilla/\"
2条回答
  •  既然无缘
    2020-12-19 01:14

    Figured it out. Had to use $null for the prefix of the default namespace ($null is equivalent to String.Empty in C#).

    Working code:

        [System.Xml.XmlNamespaceManager] $nsmgr = $xml.NameTable;
        $nsmgr.AddNamespace($null,'http://embassy/schemas/dudezilla/');
        $nsmgr.AddNamespace('xlink','http://www.w3.org/1999/xlink');
    
        [System.Xml.XmlNodeList] $nodelist;
        [System.Xml.XmlElement] $root = $xml.DocumentElement;
        $nodelist = $root.SelectNodes("//image/@xlink:href", $nsmgr);
    
        Foreach ($xmlnode in $nodelist)
        {
            $xmlnode.Value;
        }
    

提交回复
热议问题