PowerShell ändert den Typ des Rückgabeobjekts

Ich verwende PowerShell v3 und Windows PowerShell ISE. Ich habe die folgende Funktion, die gut funktioniert:

function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
    # If a Namespace URI was not given, use the Xml document's default namespace.
    if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }   

    # In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
    [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
    $xmlNsManager.AddNamespace("ns", $NamespaceURI)

    [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter

    # Try and get the node, then return it. Returns $null if the node was not found.
    $node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
    return $node
}

Jetzt erstelle ich einige ähnliche Funktionen, also möchte ich die ersten drei Zeilen in eine neue Funktion aufteilen, damit ich sie nicht überall kopieren und einfügen muss.

function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "")
{
    # If a Namespace URI was not given, use the Xml document's default namespace.
    if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }   

    # In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
    [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
    $xmlNsManager.AddNamespace("ns", $NamespaceURI)
    return $xmlNsManager
}

function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
    [System.Xml.XmlNamespaceManager]$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
    [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter

    # Try and get the node, then return it. Returns $null if the node was not found.
    $node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
    return $node
}

Das Problem ist, dass beim Ausführen von "return $ xmlNsManager" der folgende Fehler ausgegeben wird:

Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Xml.XmlNamespaceManager".

Obwohl ich meine $ xmlNsManager-Variablen explizit in den Typ System.Xml.XmlNamespaceManager umgewandelt habe, konvertiert PowerShell sie von der Get-XmlNamespaceManager-Funktion in ein Object-Array.

Wenn ich den von der Funktion Get-XmlNamespaceManager zurückgegebenen Wert nicht explizit in System.Xml.XmlNamespaceManager umwandle, wird der folgende Fehler von der Funktion .SelectSingleNode () ausgegeben, da der falsche Datentyp an den 2. Parameter der Funktion übergeben wird.

Cannot find an overload for "SelectSingleNode" and the argument count: "2".

Aus irgendeinem Grund behält PowerShell den Datentyp der Rückgabevariablen nicht bei. Ich möchte wirklich, dass dies von einer Funktion funktioniert, damit ich diese 3 Zeilen nicht überall kopieren und einfügen muss. Anregungen sind willkommen. Vielen Dank.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage