[Array] becomes [ArrayList] when imported via Import-CliXml - by Ronald Rink

Status : 


Sign in
to vote
ID 785187 Comments
Status Active Workarounds
Type Bug Repros 2
Opened 4/20/2013 5:43:09 AM
Access Restriction Public


When using Export-CliXml/Import-CliXml to serialise/deserialise objects the object [Array] becomes an [ArrayList].
The problem with that is that the methods and members of both datatype do not behave exactly the same. For example, the "Length" property returns the number of entries (Count) on [Array] and the individual length of the the list items on [ArrayList]:

PS > $a = @("11", "22");
PS > $a.Length;

$al = New-Object System.Collections.ArrayList($null);
PS > $al.Add("11");
PS > $al.Add("22");
PS > $al.Length;

The following code demonstrates the error:

PS > $ht = @{};
PS > $ht.Array = @();
PS > $ht.Array.GetType();

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS > $ht | Export-Clixml ht.xml;
PS > $ht2 = Import-Clixml ht.xml;
PS > $ht2.Array.GetType();

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ArrayList                                System.Object

The via "Export-CliXml" generated XML file still contains the correct type information:
<Objs Version="" xmlns="http://schemas.microsoft.com/powershell/2004/04">
 <Obj RefId="0">
 <TN RefId="0">
 <S N="Key">Array</S>
 <Obj N="Value" RefId="1">
 <TN RefId="1">
 <LST />

Import-CliXml already breaks "ordered hashtables" as reported under "OrderedDictionary becomes Hashtable when exported to and imported from clixml" [https://connect.microsoft.com/PowerShell/feedback/details/771721/ordereddictionary-becomes-hashtable-when-exported-to-and-imported-from-clixml]

It does not matter if you are using "Set-StrictMode" with any PowerShell version number. See also "[Array] becomes [ArrayList] when imported via Import-CliXml" [http://d-fens.ch/2013/04/20/array-becomes-arraylist/trackback/].
Sign in to post a comment.
Posted by SSG31415926 on 5/21/2014 at 6:24 AM
There are other inconsistencies. For example

Import-CliXml -Path $thisFilePath | measure


Count    : 1


(Import-CliXml -Path $thisFilePath) | measure

returns a count that depends on the number of objects in the file.

Compare that with Import-CSV, both

Import-Csv -Path $testCSVPath | measure


(Import-Csv -Path $testCSVPath) | measure

return a count based on the number of rows in the CSV.