【发布时间】:2022-01-08 18:59:49
【问题描述】:
我是探索 XML 到 Powershell 的新手。 我这里有一个示例 XML,我需要编辑它的值。
示例 XML
<Configuration>
<System>
<Address link = "http://www.thispage.com" page = "first" />
<Address link = "http://www.thispage.com" page = "second"/>
</System>
</Configuration>
这是我编辑 XML 时的“应该是结果”。
<Configuration>
<System>
<Address link = "https://www.thispage.com" page = "first" />
<Address link = "https://www.thispage.com" page = "second"/>
</System>
</Configuration>
我试过这段代码:
$fileName = "C:\Project\XML-file\SampleXML.config"
$xmlContent = [xml](Get-Content $fileName)
$xmlContent | Select-Xml -XPath 'Configuration/system/Address' | ForEach-Object {$_.node.link -replace 'http','https'}
$xmlContent.save($fileName)
在这段代码中,值没有被替换,但是文件正在被保存(文件没有变化)。但我可以在 Powershell 控制台中看到 http 被替换为 https
我也试过 SetAttribute 命令,但它正在替换链接的整个值。 例如:
<Configuration>
<System>
<Address link = "https" page = "first" />
<Address link = "https" page = "second"/>
</System>
</Configuration>
感谢您的投入!
【问题讨论】:
标签: xml powershell