C# 将powershell对象另存为xml并在c中加载#

C# 将powershell对象另存为xml并在c中加载#,c#,xml,powershell,dataset,C#,Xml,Powershell,Dataset,我有以下powershell脚本,它扫描一个位置并将文件详细信息添加到xml文件中 Get-ChildItem -recurse c:\DATA | Select-Object * , @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }},@{Name="Age";Expression={ (((Get-Date) - $_.CreationTime).Days) }} | Export-Clixml c:\DATA\Fi

我有以下powershell脚本,它扫描一个位置并将文件详细信息添加到xml文件中

Get-ChildItem -recurse c:\DATA | Select-Object  * , @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }},@{Name="Age";Expression={ (((Get-Date) - $_.CreationTime).Days) }}  | Export-Clixml c:\DATA\Final.xml 
据我所知,这应该是.net framework中的一个对象,假设是一个数据集? 我想做的是将这个对象加载到c#应用程序中,并将其用作数据集

如何将对象加载到c#中的数据集中

  • 添加对
    System.Management.Automation.dll
    程序集的引用
  • 创建PowerShell运行空间
  • 打开
    运行空间
  • 使用
    Import-CliXml
    命令创建PowerShell
    Pipeline
    对象
  • 调用
    管道
  • 关闭
    运行空间

        var rs = RunspaceFactory.CreateRunspace();
        rs.Open();
        var pl = rs.CreatePipeline(@"Import-CliXml c:\DATA\Final.xml;");
        var result = pl.Invoke();
        rs.Close();
    

  • 更快的方法可能是:

    -对于列表

    object[] xmlAsList =
         System.Management.Automation.PSSerializer.DeserializeAsList(System.IO.File.ReadAllText(@"C:\Users\batman\Desktop\myList.clixml"));
    
    -对于泛型对象

    object importedObject =
     System.Management.Automation.PSSerializer.Deserialize(System.IO.File.ReadAllText(@"C:\Users\batman\Desktop\myObject.clixml"));
    

    然后,使用
    Members
    属性通过:
    System.Management.Automation.PSObject
    管理结果。

    使用Export-CliXML可以序列化数据。因此,当您反序列化此对象时,它将不是活动对象。对象上可用的所有方法都将丢失。