Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why I am getting a Cannot convert value "System.Object[]" to type "System.Xml.XmlDocu

Status
Not open for further replies.

Ken Shaw

Programmer
Sep 12, 2023
2
US
I have tried multiple ways to insure I have a true XML object but I get this error:

Get-XmlNodes : Cannot process argument transformation on parameter 'XmlDocument'. Cannot
convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "The specified node
cannot be inserted as the valid child of this node, because the specified node is the wrong
type."
At C:\Users\OrderFile.ps1:155 char:19
+ ... Get-XmlNodes($XmlDocument, $ElementPath, $FullNPID, $NodeSeparatorCha ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: :)) [Get-XmlNodes], ParameterBindingArgumentTransfor
mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-XmlNodes

Here are some of the ways I tried to overcome the issue, but none of them solve my issue.

Creating the Xml object:
Code:
[xml]$XmlDocument = [xml](Get-Content -Path C:\Users\xml.xml -Raw)
[System.Xml.XmlDocument]$XmlDocument = [System.Xml.XmlDocument](Get-Content -Path C:\Users\xml.xml)
Code:
$XmlDocument = [xml](Get-Content -Path C:\Users\xml.xml -Raw)
$XmlDocument = [System.Xml.XmlDocument](Get-Content -Path C:\Users\xml.xml)
Code:
$XmlDocument = Get-Content -Path "C:\Users\xml.xml"
$XmlDocument = [xml]($Content -join "")
Code:
$ReferenceXML = [xml]''
$ReferenceXML.Load($("C:\Users\xml.xml"))
Call function:
Code:
$trueXml = Get-XmlNodes([REF]($XmlDocument,[string] $ElementPath,[string] $FullNPID,[string] $NodeSeparatorCharacter)
$trueXml = Get-XmlNodes([xml]($XmlDocument,[string] $ElementPath,[string] $FullNPID,[string] $NodeSeparatorCharacter)
$trueXml = Get-XmlNodes([System.Xml.XmlDocument]$XmlDocument,[string] $ElementPath,[string] $FullNPID,[string] $NodeSeparatorCharacter)
$trueXml = Get-XmlNodes($XmlDocument, $ElementPath, $FullNPID, $NodeSeparatorCharacter)
Function Parameters:

Code:
function Get-XmlNodes([ xml ]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.'){...}
function Get-XmlNodes([System.Xml.XmlDocument]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.'){...}

If anyone has any idea why I am getting this error please let me know.
 
In your Call function, check both of the top two lines. Each expression has 2 opening parentheses but only a single closing parentheses.

Code:
$trueXml = Get-XmlNodes[COLOR=#EF2929][b]([/b][/color][REF][COLOR=#EF2929][b]([/b][/color]$XmlDocument,[string] $ElementPath,[string] $FullNPID,[string] $NodeSeparatorCharacter[COLOR=#EF2929][b])[/b][/color]
$trueXml = Get-XmlNodes[COLOR=#EF2929][b]([/b][/color][xml][COLOR=#EF2929][b]([/b][/color]$XmlDocument,[string] $ElementPath,[string] $FullNPID,[string] $NodeSeparatorCharacter[COLOR=#EF2929][b])[/b][/color]

Hope this helps...
 
As I am new to PowerShell I found I had many issues but the main one was when calling the function incorrectly:

Get-XmlNodes([System.Xml.XmlDocument]$XmlDocument,[string] $ElementPath,[string] $FullNPID,[string] $NodeSeparatorCharacter) $trueXml = Get-XmlNodes($XmlDocument, $ElementPath, $FullNPID, $NodeSeparatorCharacter)

When you call functions like the above. You are basically sending all the arguments as the first parameter.
Proper ways to call them are listed below

# test function
function Get-Something {
Param(
[parameter(ValueFromPipelineByPropertyName)]
$FirstParam,

[parameter(ValueFromPipelineByPropertyName)]
$SecondParam
)

process{
Write-Host FirstParam: $FirstParam SecondParam: $SecondParam
}
}
preferred to be verbose

# named parameters
Get-Something -firstparam firstarg - secondparam secondarg
next option is positional parameters

# positional parameters
Get-Something firstarg secondarg
Finally the pipeline

# pipeline
[PSCustomObject]@{FirstParam = 'firstarg';SecondParam = 'secondarg'} | Get-Something

All of these output
FirstParam: firstarg SecondParam: secondarg

When we call it incorrectly with parenthesis

Get-Something ('firstarg', 'secondarg')

FirstParam: firstarg secondarg SecondParam:
Both arguments went as the first parameter.
 
Well... I wish you all the best solving your issue.

All I know is that I fed your code into a smart IDE and it immediately flagged a problem of missing parantheses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top