使用Powershell替换XML中的字符串会导致MALFORMED XML

我有一个相当大的 XML文件,我需要替换其中的一些连接字符串.

我使用以下代码替换字符串:

$temp = Get-Content .\bigxmlfile.xml
$temp.replace("STRING1","STRING2") | out-file .\bigxmlfile.xml -force

这样就可以很好地改变字符串,但由于某种原因,总是会破坏XML.
我无法弄清楚原因.

Out-File认编写Unicode文件.使用-Encoding来修复它:
$temp = Get-Content .\bigxmlfile.xml
$temp.replace("STRING1","STRING2") | out-file .\bigxmlfile.xml -force -encoding ascii

或者,使用Set-Content:

$temp = Get-Content .\bigxmlfile.xml
$temp.replace("STRING1","STRING2") | set-content .\bigxmlfile.xml -force

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念