I'm trying to parse following XML from a file using Powershell without actually loading it as XML document using [xml] since the document contain errors.
<data>
<company>Walter & Cooper</company>
<contact_name>Patrick O'Brian</contact_name>
</data>
To load document successfully I need to fix errors by replacing special characters as follows
& with &
< with <
' with ' etc..
I know I could do something like this to find and replace characters in a document
(Get-Content $fileName) | Foreach-Object {
$_-replace '&', '&' `
-replace "'", "'" `
-replace '"', '"'} | Set-Content $fileName
But this will replace characters everywhere in the file, I'm only interest in checking for characters inside xml tags like <company> and replacing them with xml safe entities so that resultant text is a valid document which I can load using [xml].