Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell-更新计数节点下的XML文本_Xml_Powershell - Fatal编程技术网

Powershell-更新计数节点下的XML文本

Powershell-更新计数节点下的XML文本,xml,powershell,Xml,Powershell,我有一个如下所示的XML文件 <Transactions> <Transaction Type="Login"> <LoginSetting>blahblah</LoginSetting> </Transaction> <Transaction Type="Search"> <Parameters>blahblah</Parameters>

我有一个如下所示的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>
我正在尝试更新“Count”节点下的值

由于可以运行.Count来获取名为“Transaction”的节点数,因此powershell会向我提供此错误输出

'Count' is a ReadOnly property.
是否有其他方法更新“计数”节点下的值?

这是我的解决方法: 我将xml数据导入到两个变量中——一个作为平面文本,另一个作为xml

$xml_flat = (get-content $xml_path)
[xml]$xml = (get-content $xml_path)
我从xml变量中提取了“Count”的值(因为它不一定具有显示的“Setting”值)

我对flat变量运行了一个.replace来更新我需要的设置

$xml_flat = $xml_flat.replace("<Count>$count</Count>","<Count>NewSetting</Count>")
是的,这是一种“按设计”的错误,您将获得合成“计数”属性的值(即,
$xml.Transactions.Transaction
)中有多少项)。使用
selectxml
SelectSingleNode()
获取对实际
节点的引用
$xml_flat = (get-content $xml_path)
[xml]$xml = (get-content $xml_path)
$count = Select-Xml -XML $xml -XPath "//Count"
$xml_flat = $xml_flat.replace("<Count>$count</Count>","<Count>NewSetting</Count>")
[xml]$xml_new = $xml_flat