This code:
$test = @"
<Test>
<Child>Hello</Child>
<Child>World</Child>
</Test>
"@
$xml = [xml]$test
$xml.Test.Child[1]
yields:
World
Then this code:
$xml.Test.Child[1] = "StackOVerflow"
$xml.InnerXml
yields:
<Test><Child>Hello</Child><Child>World</Child></Test>
Why the second Child node is not getting updated from World to StackOverflow?
Okay, let's try something different:
$ipaddress=([System.Net.DNS]::GetHostAddresses($hostname)|Where-Object {$_.AddressFamily -eq "InterNetwork"} | select-object IPAddressToString)[0].IPAddressToString
Now I wonder what the type of $ipaddress is:
$ipaddress.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Apparently it's string.
$xml.Test.ChildNodes[1].'#text' = $ipaddress
Cannot set "#text" because only strings can be used as values to set XmlNode properties.
At line:1 char:1
+ $xml.Test.ChildNodes[1].'#text' = $ipaddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueException
+ FullyQualifiedErrorId : XmlNodeSetShouldBeAString
But $ipaddress is string!
This finally works:
$xml.Test.ChildNodes[1].'#text' = [string]$ipaddress
What's going on?