2
此代码:更新数组元素
$test = @"
<Test>
<Child>Hello</Child>
<Child>World</Child>
</Test>
"@
$xml = [xml]$test
$xml.Test.Child[1]
收率:
World
然后,代码:
$xml.Test.Child[1] = "StackOVerflow"
$xml.InnerXml
收率:
<Test><Child>Hello</Child><Child>World</Child></Test>
为什么第二个Child
节点没有从World
更新为StackOverflow
?
好吧,让我们尝试不同的东西:
$ipaddress=([System.Net.DNS]::GetHostAddresses($hostname)|Where-Object {$_.AddressFamily -eq "InterNetwork"} | select-object IPAddressToString)[0].IPAddressToString
现在我不知道的$ipaddress
是什么类型:
$ipaddress.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
显然,这是字符串。
$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
但$ipaddress
是字符串!
这最后的工作原理:
$xml.Test.ChildNodes[1].'#text' = [string]$ipaddress
这是怎么回事?
在PowerShell中相同的行为6.0.0-alpha.13 –
谢谢你这一点,但你可能已经注意到了,我已经在原始问题本身中发布了相同的工作。 –