Export-Clixml Escape All Special Characters in Variable

955 views Asked by At

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.

3

There are 3 answers

0
1housand On BEST ANSWER

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

5
colsw On

Specify the encoding in the Export-Clixml cmdlt.

using Export-Clixml -Path $path -Encoding ASCII should resolve issues with incorrectly stored characters, Unicode and UTF8 are also available as EncodingTypes.

2
briantist On

Based on your edit, I think what's happening is that certain characters are being converted into XML entities. For example — would take the place of a character with the code 0x97.

You don't need to worry about this; the whole point of serializing an object into XML is to deserialize it back into an object.

By using Import-Clixml on the file that is generated, you should see the same object (it just won't be in a "live" state anymore; this matters for some types of objects as parts of them just won't work anymore).