在 XML 文档中查找和替换字符串
Find and replace strings in XML doc
我有一个 XML 文件,想在 XML 文档中找到所有 "en-us" 字符串并替换为不同的区域性。我能找到它们,但不知道如何更换。尝试替换、获取内容和设置内容,但没有用。可能是我没用对
$testPath = "C:\temp\testing\"
$path = Get-ChildItem $testPath -Recurse
ForEach ($file in $path){
$file | Select-String -AllMatches "en-us" | % { $_.Matches }
}
这是摘自我评论中的link。
$testpath = "C:\Temp\z" ;$path=gci $testpath -Recurse
$path.fullname | ForEach-Object {(GC $_ -Raw).Replace('line2','NewLine2')| Set-Content $_}
我发现这是一种替换文本文件中值的快速方法:
[System.IO.File]::WriteAllText(
'C:\path\to\export-file.xml',
([System.IO.File]::ReadAllText('C:\path\to\import-file.xml') -replace 'en-us', 'new value'),
(New-Object System.Text.UTF8Encoding($false))
)
它将读取 import-file.xml 中的所有内容,将 'en-us' 替换为 'new value' 并使用 UTF8 无 BOM 编码保存到 export-file.xml。正如 Ansgar 在上面的评论中指出的那样,我不能保证这不会破坏你的 xml.
我有一个 XML 文件,想在 XML 文档中找到所有 "en-us" 字符串并替换为不同的区域性。我能找到它们,但不知道如何更换。尝试替换、获取内容和设置内容,但没有用。可能是我没用对
$testPath = "C:\temp\testing\"
$path = Get-ChildItem $testPath -Recurse
ForEach ($file in $path){
$file | Select-String -AllMatches "en-us" | % { $_.Matches }
}
这是摘自我评论中的link。
$testpath = "C:\Temp\z" ;$path=gci $testpath -Recurse
$path.fullname | ForEach-Object {(GC $_ -Raw).Replace('line2','NewLine2')| Set-Content $_}
我发现这是一种替换文本文件中值的快速方法:
[System.IO.File]::WriteAllText(
'C:\path\to\export-file.xml',
([System.IO.File]::ReadAllText('C:\path\to\import-file.xml') -replace 'en-us', 'new value'),
(New-Object System.Text.UTF8Encoding($false))
)
它将读取 import-file.xml 中的所有内容,将 'en-us' 替换为 'new value' 并使用 UTF8 无 BOM 编码保存到 export-file.xml。正如 Ansgar 在上面的评论中指出的那样,我不能保证这不会破坏你的 xml.