【发布时间】:2020-03-28 10:31:15
【问题描述】:
我有一个类似这样的 XML 文件
<Transactions>
<Transaction Type="Login">
<LoginSetting>blahblah</LoginSetting>
</Transaction>
<Transaction Type="Search">
<Parameters>blahblah</Parameters>
<Count>Setting</Count>
</Transaction>
<Transaction Type="Logout">
<LogoutSetting>blahblah</LogoutSetting>
</Transaction>
</Transactions>
此文件的路径存储在 $xml_path 下,使用 powershell 我已将这些设置导入 [xml]$xml
[xml]$xml = (get-content $xml_path)
我正在尝试更新“计数”节点下的值
$xml.Transactions.Transaction.Count = 'NewSetting'
由于可以运行 .Count 来获取名为“Transaction”的节点数,因此 powershell 会为我提供此错误输出
'Count' is a ReadOnly property.
还有其他方法可以更新“Count”节点下的值吗?
【问题讨论】:
-
是的,这是一种“按设计”的错误,您将获得合成“计数”属性的值(即
$xml.Transactions.Transaction中有多少项目)。使用Select-Xml或SelectSingleNode()获取对实际<Count>节点的引用
标签: xml powershell