xmlpowershellreplaceselectnodes

powershell - replace characters between xml nodes


I have an xml file that I need to prepend replace certain characters with a back slash - the app that takes the xml needs it for various reasons.

So I need to do the following chnages " to be replaced by \" backslash \ to be replaced by \ form feed to be replaced by \f newline to be replaced by \n carriage return to be replaced by \r tab to be replaced by \t backspace to be replaced by \b

so a very simple xml snippet looks like this.

<?xml version="1.0" encoding="UTF-8"?>
      <ContactDetailsRow num="1">
        <Notes>This a test for special characters that need to be replaced
Ampersand - &amp;
Double Quote - &quot;
Forward Slash - /
Back Slash - \
Less Than - &lt;
Greater Than - &gt;
Question Mark - ?
Tab     </Notes>
      </ContactDetailsRow>

What I have tried so far (based on GC with a replace does) not seem to work.

$path = "C:\Dump\TEST"
$Files = Get-Childitem -path $path -File -include testfile*.xml -name

foreach ($File in $Files)
{
    write-host "=== FILE is $File ===" -ForegroundColor "White"
    $xml = [xml](Get-Content $path\$File)

    $xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '&quot;', '\&quot;'   #doubel quote
    $xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`n;', '`\n'          #newline
    $xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`f' , '`\f'          #form feed
    $xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`r' , '`\r'          #carriage return
    $xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`t' , '`\t'          #tab
    $xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`b' , '`\b'          #backspace
    $xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes -replace '`\' , '`\\'           #back slash
    write-host $xml.ContactDetailsRow.Notes

    $xml.Save("$path\$File")
}   

I did also try to do it with a select node but that didnt work either.

$xml.SelectNodes('//text()') | ForEach-Object {  
    $_.Value = ($_.Value -replace "&quot;" , "\&quot;")
}

Solution

  • XML node values are from type String. You can just use the .Replace() method.

    $xml.ContactDetailsRow.Notes = $xml.ContactDetailsRow.Notes.Replace("`t" , '\t')
    

    Also Powershell interprets expressions like &quot; if you read file-content as XML.