0
我有一個xml文件,我需要預先用反斜槓替換某些字符 - 需要xml的應用程序需要它,原因有很多。powershell - 替換xml節點之間的字符
所以我需要通過\ " 反斜槓\做到以下幾點chnages "被替換由\ 換更換由\更換˚F 換行符由\ n 回車替換成替換爲\ r 選項卡將被\ t 退格替換爲\ b
所以一個非常簡單的xml片段看起來像這樣。
<?xml version="1.0" encoding="UTF-8"?>
<ContactDetailsRow num="1">
<Notes>This a test for special characters that need to be replaced
Ampersand - &
Double Quote - "
Forward Slash -/
Back Slash - \
Less Than - <
Greater Than - >
Question Mark - ?
Tab </Notes>
</ContactDetailsRow>
我到目前爲止試過的(基於GC的替換)似乎沒有工作。
$path = "C:\Dump\TEST"
$Files = Get-Childitem -path $path -File -include testfile*.xml -name
foreach ($File in $Files)
{
write-host "=== FILE is $File ===" -ForegroundColor "White"
$xml = [xml](Get-Content $path\$File)
$xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '"', '\"' #doubel quote
$xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`n;', '`\n' #newline
$xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`f' , '`\f' #form feed
$xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`r' , '`\r' #carriage return
$xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`t' , '`\t' #tab
$xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`b' , '`\b' #backspace
$xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`\' , '`\\' #back slash
write-host $xml.ContactDetailsRow.Notes
$xml.Save("$path\$File")
}
我也嘗試用選擇節點,但沒有工作做,要麼。
$xml.SelectNodes('//text()') | ForEach-Object {
$_.Value = ($_.Value -replace """ , "\"")
}