Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
我无法在xml PowerShell中保存更改_Powershell - Fatal编程技术网

我无法在xml PowerShell中保存更改

我无法在xml PowerShell中保存更改,powershell,Powershell,我需要用xml更新一行并保存更改。这看起来很容易,但对我不起作用,我找不到我的错误( 所以,我需要帮助。 这是我的代码: [xml]$XmlDocument = Get-Content -Path "C:\Users\Administrator\Documents\Practise\Test.xml" $random = -join ((48..57) + (97..122) | Get-Random -Count 16 | % {[char]$_}) $node = $XmlDocument

我需要用xml更新一行并保存更改。这看起来很容易,但对我不起作用,我找不到我的错误( 所以,我需要帮助。 这是我的代码:

[xml]$XmlDocument = Get-Content -Path "C:\Users\Administrator\Documents\Practise\Test.xml"
$random = -join ((48..57) + (97..122) | Get-Random -Count 16 | % {[char]$_})

$node = $XmlDocument.Payment.PaymentOptions.Bank.Value5
$node = $node.Replace('13384wL839', $random)

$XmlDocument.Save("C:\Users\Administrator\Documents\Practise\Test.xml")
这是我的xml:

<Payment xmlns="2.0.0">
  <PaymentValue1>
    <value1>180.00</value1>
    <value2>2017-09-30</value2>
    <value3>022456789</value3>
  </PaymentValue1>
  <PaymentOptions>
    <Bank>
      <Value4>Test1</Value4>
      <Value5>13384wL839</Value5>
    </Bank>
  </PaymentOptions>
</Payment>

180
2017-09-30
022456789
测试1
13384wL839

我看到$node已更改,但当我保存新xml时,仍会显示旧值。

您从未实际将该值设置回文档中

$XmlDocument.Payment.PaymentOptions.Bank.Value5 = $XmlDocument.Payment.PaymentOptions.Bank.Value5.Replace('13384wL839', $random)

应该这样做。

因为新值与旧值无关,
不需要更换任何东西,
您只需设置值

$File= "C:\Users\Administrator\Documents\Practise\Test.xml"
[xml]$XmlDocument = Get-Content $File
$random = -join ((48..57) + (97..122) | Get-Random -Count 16 | % {[char]$_})

$XmlDocument.Payment.PaymentOptions.Bank.Value5 = $random

$XmlDocument.Save($File)

我还尝试在新文档中保存值。通过这种方式创建新文档,但值不会更新。$XmlDocument.Payment.PaymentOptions.Bank.Value5=$XmlDocument.Payment.PaymentOptions.Bank.Value5.Replace('13384wL839',$random)--这没有帮助。值已更改,但xml未更新更新的答案,出现错误。从我看到的当前节点没有“值”这样的属性,所以replace无法再次工作编辑后,我认为这是一个重复,因此相应地标记了$XmlDocument.Payment.PaymentOptions.Bank.Value5=$XmlDocument.Payment.PaymentOptions.Bank.Value5.replace('13384wL839',$random)--我在尝试此项时犯了一个错误。但我再试一次,它就工作了!谢谢您的帮助=)