【问题标题】:Export-Clixml converts a generic list to a standard arrayExport-Clixml 将通用列表转换为标准数组
【发布时间】:2014-05-09 22:18:22
【问题描述】:

我在脚本中使用 Collections.Generic.List[object] 而不是 PowerShell 数组,以便拥有“.add()”并且还因为速度(列表很大)。

当我通过Export-Clixml 导出列表并再次重新导入时,它不再是通用列表;这是一个标准数组,“.add()”不再起作用了。

奇怪的是(或不是),当我将另一个通用列表对象放在第一个对象中时,它会在导出-导入过程中幸存下来。 为什么以及如何解决这个问题?

$list = New-Object System.Collections.Generic.List[object]
$entry = New-Object Psobject -Property @{
    Test = "1"
    Sublist = (New-Object System.Collections.Generic.List[object])
}
$list.add($entry)

$list | Export-Clixml D:\text.xml
$newlist = import-clixml D:\text.xml


# Produces this export:

*<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="Test">1</S>
      <Obj N="Sublist" RefId="1">
        <TN RefId="1">
          <T>System.Collections.Generic.List`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]</T>
          <T>System.Object</T>
        </TN>
        <LST />
      </Obj>
    </MS>
  </Obj>
</Objs*>

【问题讨论】:

    标签: arrays powershell generic-list


    【解决方案1】:

    管道正在对您执行此操作。管道将“展开”数组和集合,但多个返回总是重新组合成数组。数组列表中的数组列表有效,因为它只展开一层深度,因此第一个(外部)数组列表“展开”,第二个(内部)数组列表完整通过。

    试试这个方法:

    Export-Clixml -InputObject $list -Path d:\text.xml
    

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 2012-02-21
      • 2018-07-05
      • 1970-01-01
      • 2011-07-11
      相关资源
      最近更新 更多