在PowerShell中加载序列化的DataTable - 提供DataRows的数组,而不是DataTable [英] Loading a serialized DataTable in PowerShell - Gives back array of DataRows not a DataTable
问题描述
我正在PowerShell中保存并加载DataTable。
我保存如下:
$ dt | Export-CliXml -pathc:\exports\data.xml
并加载它这个:
$ dt = Import-CliXml -pathc:\exports\data.xml
但是我得到的类型是一组Row而不是DataTable!
这导致我的主要问题,因为它需要被传递到一个需要一个DataTable的函数,它不能被转换为一个。
谢谢,谢谢。
这是一个已知的陷阱:PowerShell处理您的 DataTable
作为 DataRow
项目的集合。这是第一个命令
$ dt | export-CliXml -pathc:\exports\data.xml
已经忘记数据表。您可以查看输出文件,它以 DataRow
开头:
< Objs Version =1.1.0.1xmlns =http://schemas.microsoft.com/powershell/2004/04>
< Obj RefId =0>
< TN RefId =0>
< T> System.Data.DataRow< / T>
为避免这种影响,请使用,
操作员(看起来很好笑,但它正是如何工作的):
,$ dt | Export-CliXml -pathc:\exports\data.xml
因此,输出文件现在以 DataTable
开头:
< Objs Version = 1.1.0.1xmlns =http://schemas.microsoft.com/powershell/2004/04>
< Obj RefId =0>
< TN RefId =0>
< T> System.Data.DataTable< / T>
之后,您可以导入表:
$ dt = Import-CliXml -pathc:\exports\data.xml
我们来看看:
$ dt | Get-Member
#output:
TypeName:Deserialized.System.Data.DataRow
...
我们可以看到相同的效果( DataRow
而不是 DataTable
)。因此,正确的命令是使用,
:
,$ dt | Get-Member
#output:
TypeName:Deserialized.System.Data.DataTable
...
所以,我们真的以这种方式脱水了一个 DataTable
。
=== / p>
编辑:这种效果被称为展开。 PowerShell倾向于展开集合。逗号运算符创建一个单个项目的数组。 PowerShell也展开了这个数组,但它并没有展开它的项目(我们的 DataTable
)。
非常类似的问题:
I'm trying to save and load a DataTable in PowerShell. I save it like this:
$dt | Export-CliXml -path "c:\exports\data.xml"
and load it like this:
$dt = Import-CliXml -path "c:\exports\data.xml"
But the type I get back is an array of Rows rather than a DataTable! This is causing me major problems as it needs to be passed into a function which requires a DataTable, and it cannot be cast to one.
Any help greatly appreciated, thanks.
This is a known trap: PowerShell processes your DataTable
as a collection of DataRow
items. That is the very first command
$dt | Export-CliXml -path "c:\exports\data.xml"
already "forgets" the data table. You may take a look at the output file, it starts with DataRow
:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Data.DataRow</T>
To avoid this effect, use the ,
operator (it looks funny but that’s exactly how it works):
, $dt | Export-CliXml -path "c:\exports\data.xml"
As a result, the output file now starts with DataTable
:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Data.DataTable</T>
After that you can import the table back:
$dt = Import-CliXml -path "c:\exports\data.xml"
Let’s check it:
$dt | Get-Member
# output:
TypeName: Deserialized.System.Data.DataRow
…
We can see the same effect (DataRow
instead of DataTable
). Thus, the correct command is with ,
:
, $dt | Get-Member
# output:
TypeName: Deserialized.System.Data.DataTable
…
So, we really dehydrate a DataTable
in this way.
===
EDIT: This effect is known as unrolling. PowerShell tends to unroll collections. The comma operator creates an array of a single item. PowerShell unrolls this array, too, but it does not unroll its items (our DataTable
).
Here is a very similar question:
这篇关于在PowerShell中加载序列化的DataTable - 提供DataRows的数组,而不是DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!