'How to Remove Special/Bad Characters from XML Using Powershell

I have an XML Files and I want to remove those Hexadecimal Characters Errors from the file below is the invalid characters:

enter image description here

I don't know what does STX means and when i tried copying it to my clipboard and paste it in MS Work it shows some other value.

How can I write a script in powershell to remove the above from my XML file.



Solution 1:[1]

Update of function :)

function Repair-XmlString {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0,ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [string]$String
    )
    Write-Host "Cleaning string for XML parsing [String: $($String)]"
    $rPattern = "[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000\x10FFFF]"
    $cleaned = $String -replace $rPattern, ''
    Write-Host "Returning parsed string [String cleaned: $($cleaned)]"
    return $cleaned
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SCCMOG