# Function Parameter System.Xml.XmlDocument gives error

**URL:** <https://forums.powershell.org/t/function-parameter-system-xml-xmldocument-gives-error/24853>\
**Category:** PowerShell Help\
**Created:** [October 9, 2024, 1:56pm UTC](https://forums.powershell.org/t/function-parameter-system-xml-xmldocument-gives-error/24853 "2024-10-09T13:56:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bahnski](https://avatars.discourse-cdn.com/v4/letter/b/d9b06d/32.png) [@Bahnski](https://forums.powershell.org/u/Bahnski)\
**Post date:** [October 9, 2024, 1:56pm UTC](https://forums.powershell.org/t/function-parameter-system-xml-xmldocument-gives-error/24853/1 "2024-10-09T13:56:34Z")

</div>

I would like to create a script in which a function returns an XML document.  
A second function should create a sub-element for the XML document which is later appended to the main XML document.  
In my example, GenMainXML creates the main element and stores the data in the variable $NewXML. Then I want to use the second function GenSubXML to create and return an empty sub-element. To do this, I need the main element in the function, which I pass as a parameter. From then on, I always get the message:

```auto
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

```

Here my TestCode

```auto
function GenMainXML(){
    $XMLDoc = New-Object -TypeName System.Xml.XmlDocument
    $null = $XMLDoc.AppendChild($XMLDoc.CreateXmlDeclaration('1.0', 'UTF-8', 'yes'));
    $XmlData = $XMLDoc.CreateElement("Data")
    $XMLDataConfig = $XMLDoc.CreateElement("Config")
    $XMLDataConfigVersion = $XMLDoc.CreateElement("Version")
    $XMLDataConfigVersion.InnerText = "1"
    $Null = $XMLDataConfig.AppendChild($XMLDataConfigVersion)
    $XMLDataConfigDataVersion = $XMLDoc.CreateElement("DataVersion")
    $XMLDataConfigDataVersion.InnerText = "1"
    $Null = $XMLDataConfig.AppendChild($XMLDataConfigDataVersion)
    $null = $XMLData.AppendChild($XMLDataConfig)
    $Null = $XMLDoc.AppendChild($XMLData)
    $xmldoc.InnerXml
    return $XMLDoc
}
function GenSubXML(){
    Param(
        # Parameter help description
        [Parameter(ValueFromPipelineByPropertyName,Mandatory = $true,
        HelpMessage = 'XML Object')]
        $XMLSource
        )
        write-host "GenSubXML"
        $XMLSource.InnerXml
        $XMLTitle= $XMLSource.CreateElement("Title")
        $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("ID"))
        $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("Title"))
        $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("ExtraTitle"))
        return $XMLTitle
}
$NewXML = GenMainXML
$NewSubXML = GenSubXML -XMLSource $NewXML
$NewXML.Data.AppendChild($NewSubXML)

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [October 9, 2024, 2:53pm UTC](https://forums.powershell.org/t/function-parameter-system-xml-xmldocument-gives-error/24853/2 "2024-10-09T14:53:22Z")

</div>

Hello. I’m unable to determine what your question is. Please help us help you.

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [October 9, 2024, 3:47pm UTC](https://forums.powershell.org/t/function-parameter-system-xml-xmldocument-gives-error/24853/3 "2024-10-09T15:47:36Z")

</div>

I think you may have inadvertently withheld some useful information. When loading your functions and executing them as you described, this is actually the complete error text:

```auto
Method invocation failed because [System.String] does not contain a method named 'CreateElement'.
At line:10 char:9
+ $XMLTitle= $XMLSource.CreateElement("Title")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
Method invocation failed because [System.String] does not contain a method named 'CreateElement'.
At line:11 char:9
+ $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("ID"))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [System.String] does not contain a method named 'CreateElement'.
At line:12 char:9
+ $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("Title ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [System.String] does not contain a method named 'CreateElement'.
At line:13 char:9
+ $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("Extra ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

```

It would appear at first glance this is because once you define $XMLSource it is a string object rather than XML. This looks like it’s because your first function GenMainXML is returning an array of string text rather than an XML object.  
If I change the parameter on your second function to strongly type the input as XML:

```Powershell
Param(
        # Parameter help description
        [Parameter(ValueFromPipelineByPropertyName,Mandatory = $true,
        HelpMessage = 'XML Object')]
        [XML]$XMLSource
        )

```

It fails with a different error providing another clue

```auto
GenSubXML : Cannot process argument transformation on parameter 'XMLSource'. 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 line:1 char:35
+ $NewSubXML = GenSubXML -XMLSource $NewXML
+ ~~~~~~~
    + CategoryInfo : InvalidData: (:) [GenSubXML], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,GenSubXML

```

The issue is with the last two lines of your GenMainXML:

```Powershell
    $xmldoc.InnerXml
    return $XMLDoc

```

your function returns two objects. String text, followed by your XML object. This means you end up with an array of objects for output, which ruins what your other function is expecting.

This code now executes without error. Whether or not it does what you intended I’m not sure:

```Powershell
function GenMainXML(){
    $XMLDoc = New-Object -TypeName System.Xml.XmlDocument
    $null = $XMLDoc.AppendChild($XMLDoc.CreateXmlDeclaration('1.0', 'UTF-8', 'yes'));
    $XmlData = $XMLDoc.CreateElement("Data")
    $XMLDataConfig = $XMLDoc.CreateElement("Config")
    $XMLDataConfigVersion = $XMLDoc.CreateElement("Version")
    $XMLDataConfigVersion.InnerText = "1"
    $Null = $XMLDataConfig.AppendChild($XMLDataConfigVersion)
    $XMLDataConfigDataVersion = $XMLDoc.CreateElement("DataVersion")
    $XMLDataConfigDataVersion.InnerText = "1"
    $Null = $XMLDataConfig.AppendChild($XMLDataConfigDataVersion)
    $null = $XMLData.AppendChild($XMLDataConfig)
    $Null = $XMLDoc.AppendChild($XMLData)
    return $XMLDoc
}
function GenSubXML(){
    Param(
        # Parameter help description
        [Parameter(ValueFromPipelineByPropertyName,Mandatory = $true,
        HelpMessage = 'XML Object')]
        $XMLSource
        )
        write-host "GenSubXML"
        $XMLTitle= $XMLSource.CreateElement("Title")
        $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("ID"))
        $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("Title"))
        $Null = $XMLTitle.AppendChild($XMLSource.CreateElement("ExtraTitle"))
        return $XMLTitle
}
$NewXML = GenMainXML
$NewSubXML = GenSubXML -XMLSource $NewXML
$NewXML.Data.AppendChild($NewSubXML)

```

---

<div class="post-metadata">

**Author:** ![Bahnski](https://avatars.discourse-cdn.com/v4/letter/b/d9b06d/32.png) [@Bahnski](https://forums.powershell.org/u/Bahnski)\
**Post date:** [October 10, 2024, 6:03am UTC](https://forums.powershell.org/t/function-parameter-system-xml-xmldocument-gives-error/24853/4 "2024-10-10T06:03:44Z")

</div>

It was a mistake on my part. I had the values ​​output in the function without a write host and forgot that this would automatically return the value. Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 9, 2024, 6:04am UTC](https://forums.powershell.org/t/function-parameter-system-xml-xmldocument-gives-error/24853/5 "2024-11-09T06:04:28Z")

</div>

Closed on November 9, 2024, 6:04am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
