Understanding the difference between System.Object[] System.Xml.XmlDocument

I keep running into this error message when I tray to pass and xml to a function:
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.”

#I have tried many different declarations:
#Option 1
$XmlDocument=New-Object System.XML.XMLDocument
$XmlDocument.Load("C:\Users\xml.xml")
#Option 2
$XmlDocument = (Get-Content -Path "C:\Users\xml.xml")

#Forcing the object being passed to xml:
$Test = Get-XmlNamespaceManager([ xml ]$XmlDocument)
$Test = Get-XmlNamespaceManager([system.xml.xmlelement]$XmlDocument)

#Forcing the object being received to xml:
function Get-XmlNamespaceManager([ xml ]$XmlDocument){}
function Get-XmlNamespaceManager2([system.xml.xmlelement]$XmlDocument){}

The only way I have got it to work is if I just pass the string of the file name and run the .Load, .LoadXml, or Get-Content in the function.
I really want to understand how to pass the xml object to a function without PowerShell throwing an error.
OK I have put on my dunce cap and am anxiously awaiting lessons from all the PowerShell masters out there.

Thanks in advance.

When you post code or ERROR MESSAGES please format it as code.

This usually happens when you pass an array instead of a single element.

$Content = Get-Content -Path "Path\to\file"
$xml = [xml]($Content -join "")

Even one line would be enough

[XML]$xml = Get-Content -Path  'Path\to\file'

All of the above answers still gives me the same error:

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."

Still gives me the same error:

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."

Testing some more and found that if I do this:

[string]$XmlDocument = 'dog'
$XmlDocument = Get-Content -Path 'C:\Users\xml.xml'
Write-Host $XmlDocument

The console displays the xml doc with the look of a string.
When you most over the $XmlDocument in debug mode it displays a single line box with the whole xml text in it.

If I do this:
[xml]$XmlDocument = [xml]‘’

$XmlDocument = Get-Content -Path “C:\Users\xml.xml”
Write-Host $XmlDocument

When you most over the $XmlDocument with debug mode it displays a box with

$XmlDocument = |
xml NameSpace |
— ---------------- |

version=“1.0” encoding-“utf-8” NameSpace
Sorry could not upload images
The console writes just a few blank lines.

either way I still get the following error:

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."

My questions are:

  1. What should be displayed when mouse over object in debug mode?
  2. What should be written to host when I have an xml object?
  3. An explanation of why PowerShell will not see it as an xml object?

Try:

Write-Host $XmlDocument.InnerXML 
or
Write-Host $XmlDocument.OuterXML

Thanks for that one, I did figure it out to send xml to console but my real issue is why do I get the following 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:

[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)

$XmlDocument = [xml](Get-Content -Path C:\Users\xml.xml -Raw) $XmlDocument = [System.Xml.XmlDocument](Get-Content -Path C:\Users\xml.xml)

$XmlDocument = Get-Content -Path "C:\Users\xml.xml" $XmlDocument = [xml]($Content -join "")

$XmlDocument = [xml]'' $XmlDocument .Load($("C:\Users\xml.xml"))

Call function:

$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:

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

Don’t you get error messages using these code lines? :thinking:

That’s not how we call functions in PowerShell.

I’d urgently recommend to (re-)read the help …

… and you may show your actual code.

I totally agree. Aside from taking a cumbersome approach to loading your XML document, we have no idea what the Get-XMLNodes function is trying to do. Maybe not the best approach, but I load XML document as such:

$objXML = New-Object System.XML.XMLDocument
$objXML.PreserveWhiteSpace = $True
$objXML.Load('C:\Full\Path\To\XML\File.xml')

Once you have your XML file loaded, you have a plethora of Properties and Methods to play with:

1 Like

And since it is obviously not an issue to actually load the file you may just provide the file path for your function and load the XML inside your function. :man_shrugging:t3:

Agreed … based on the error message, you may want to focus on the InsertBefore and or InsertAfter methods.

I was using code from the net thinking it was good but when I checked the functions they are not returning anything so the error I am getting is a red haring. It has nothing to do with the passing of the XML but in the function itself. Thank you all for your help, I learned many things on this wild goose chase.

You do not call functions like this. You are basically sending all the arguments as the first parameter. You call them like this

# 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

Now watch when we call it incorrectly with parenthesis

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

FirstParam: firstarg secondarg SecondParam:

Uh oh, both arguments went as the first parameter.

2 Likes

Excellent explanation. :+1:t3:

Thank you very much.

This is a great explanation for a noob to PowerShell thank you so much.

It’s a common pitfall. Especially for those who are familiar with C#

1 Like