xmlpowershellexport-to-xml

Export-Clixml Escape All Special Characters in Variable


I have a DataGrid ($myWindow.myDataGrid.Items) that I am trying to Export-Clixml. The $myWindow.myDataGrid.Items is an ItemCollection that contains String properties that are words with certain characters, like "C‘Thun" or "—Hello". To access the string I am currently looking at, I type $myWindow.myDataGrid.Items[0].Title and that would give me the string "C‘Thun".

The command I used was:

$myWindow.myDataGrid.Items | Export-Clixml -path $path

When it's exported, they are translated into other characters. In notepad++, the "‘" and "—" show up as "x91" and "x97" respectively. I checked the array before exporting it and the text is accurate, but after exporting, I check the XML file and the text is all converted. I need to retain all the original characters.

I then used this command, to Import-Clixml back into my DataGrid:

$Global:items = [Object[]]Import-Clixml -path $path
$myWindow.myDataGrid.ItemsSource = $Global:items

I put a breakpoint at $Global:items = [Object[]]Import-Clixml -path $path to see what the value at $Global:items[0].title is when it gets imported and sure enough, it is a ?. And the values in the DataGrid are also ?.

I'm on powershell version 4.

EDIT: Changed some details. Sorry for the trouble. I am on 2 different systems and cannot copy and paste.


Solution

  • After trying everything, with the help of ConnorLSW's original reply, I decided to focus more on the encoding side of the issue and I finally found something that worked!

    After the XML file has been exported with Export-Clixml, I had to manually Get-Content and then Set-Content to UTF8 like this:

    Get-Content $path | Out-String | Set-Content $path -Encoding UTF8

    The x91 and x97 gets converted back to its original characters and all is good. Also, for some reason, the following code does not have the same effect and does not work for me:

    $myWindow.myDataGrid.Items | Export-Clixml -path $path -Encoding UTF8