1

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.

1housand
  • 508
  • 7
  • 16
  • `'` and `--` are not special characters, but even using a backtick instead of apostrophe, I can't reproduce this issue at all. Can you give an actual sample? Further, I suspect that the issue is that when you look at the XML, you are using an editor that doesn't understand the encoding of the XML file, and that the characters are encoded correctly. The real test is whether it all works correctly when you use `Import-Clixml`. Please provide further details and steps to repro. – briantist May 25 '17 at 21:51
  • @briantist sorry, for the misunderstanding. I have updated it with more accurate information. – 1housand May 26 '17 at 13:59

3 Answers3

0

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.

colsw
  • 3,216
  • 1
  • 14
  • 28
  • Thanks for the reply. I have already tried the different encodings, but no dice. – 1housand May 26 '17 at 14:19
  • @fanterrific could you give me the output of `[Convert]::ToBase64String(([Text.Encoding]::Unicode.GetBytes($StringThatFails)))` so I can see exactly what is failing? definitely a strange one if it's getting dumped out to `?` in unicode. – colsw May 26 '17 at 14:32
  • I just updated my post with the exact characters that are failing (‘ and —) so maybe you can try it yourself by copy&pasting my string examples. In the meantime, I will try to run that command and get that value for you. thanks. – 1housand May 26 '17 at 14:43
  • I got `GCA=` for `‘` and `FCA=` for `—` – 1housand May 26 '17 at 14:53
  • I had to manually ingest it as a string and encode it to UTF8. I posted details in my answer. thank you! – 1housand May 26 '17 at 15:54
0

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).

briantist
  • 45,546
  • 6
  • 82
  • 127
  • Thank you, I updated my original post again with the steps I take after exporting them. I actually already do what you're recommending, but it's not translating back correctly. – 1housand May 26 '17 at 14:13
  • @fanterrific you might want to try serializing the entire data grid object or something, see if that works..? Otherwise I think you need to post some XML that demonstrates the problem, or some code we can actually run. – briantist May 26 '17 at 14:20
0

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

1housand
  • 508
  • 7
  • 16