【发布时间】:2013-05-04 14:47:15
【问题描述】:
我正在尝试使用 Powershell 从文件中解析以下 XML,但实际上没有使用 [xml] 将其加载为 XML 文档,因为该文档包含错误。
<data>
<company>Walter & Cooper</company>
<contact_name>Patrick O'Brian</contact_name>
</data>
要成功加载文档,我需要通过替换特殊字符来修复错误,如下所示
& with &
< with <
' with ' etc..
我知道我可以做这样的事情来查找和替换文档中的字符
(Get-Content $fileName) | Foreach-Object {
$_-replace '&', '&' `
-replace "'", "'" `
-replace '"', '"'} | Set-Content $fileName
但这将替换文件中所有位置的字符,我只对检查
【问题讨论】:
标签: xml regex powershell