Why Error “System.String does not contain a method named ‘AppendChild'”

Question:

Why does this PowerShell Script not work:

Error is

If I replace

by

it will work, but I’d like to know why first syntax does not work because it is illogical for me. $xml.products should be a valid XML object and thus provide the method AppendChild().

Answer:

$xml.products does not reference the products node itself, but the contents of the products node. Since products is empty, it evaluates to an empty string.

To get to the products node you could also use:

or, more specifically:

But SelectSingleNode() will probably serve you just fine

Source:

Why Error “System.String does not contain a method named ‘AppendChild'” by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply