我有一个 PowerShell 脚本,它试图用不同的值替换 XML 文件节点中的值。
例如,在SectorDesc
节点内将 '&' 替换为 'and'。
<?xml version="1.0" encoding="UTF-8"?>
<OrganisationUnits>
<OrganisationUnitsRow num="8">
<OrganisationId>ACME24/7HOME</OrganisationId>
<OrganisationName>ACME LTD</OrganisationName>
<ContactDetails>
<ContactDetailsRow num="1">
<ContactCode>OFFICE</ContactCode>
</ContactDetailsRow>
</ContactDetails>
<Sector>P</Sector>
<SectorDesc>Private Private & Voluntary</SectorDesc>
</OrganisationUnitsRow>
</OrganisationUnits>
所以这条线
<SectorDesc>Private Private & Voluntary</SectorDesc>
现在看起来像这样
<SectorDesc>Private Private and Voluntary</SectorDesc>
到目前为止,这是我的代码。
$file = "C:\Dump\test\test.xml"
$xml = [xml](Get-Content $file)
$xml.selectNodes('//SectorDesc/value[contains(.,"text")]')|
ForEach-Object{$_.'#text' = ($_.'#text' -replace "&","and") }
$xml.Save("$File")