Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

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
At C:\Users\OrderFile.ps1:155 char:19
+ ... Get-XmlNodes($XmlDocument, $ElementPath, $FullNPID, $NodeSeparatorCha ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-XmlNodes], ParameterBindingArgumentTransfor
+ 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 --> powershell

[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]''
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.

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

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([REF]($XmlDocument,[string] $ElementPath,[string] $FullNPID,[string] $NodeSeparatorCharacter)
$trueXml = Get-XmlNodes([xml]($XmlDocument,[string] $ElementPath,[string] $FullNPID,[string] $NodeSeparatorCharacter) 

Hope this helps...

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

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 {


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.

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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close