如何在 PowerShell 中使用 XmlReader 来流式传输大/巨大的 XML 文件? [英] How can i use XmlReader in PowerShell to stream big/huge XML files?

查看:71
本文介绍了如何在 PowerShell 中使用 XmlReader 来流式传输大/巨大的 XML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几 GB 的 XML.XML 中没有空格.

I have an XML of couple of gigabytes. There are no spaces in the XML.

所以我写了一些 C# 代码来拆分单个文件(其中有一些额外的代码来执行一些操作,例如在测试时随机化)

So I wrote a little C# code to split in single files (which has some additional code to perform some stuff e.g. randomizing while testing)

using (XmlReader MyReader = XmlReader.Create(@"d:\xml\test.xml"))
            {
                while (MyReader.Read())
                {
                    switch (MyReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (MyReader.Name == "Customer")
                            {
                                XElement el = XElement.ReadFrom(MyReader) as XElement;
                                if (el != null)
                                {
                                    custNumber = (string)el.Element("CustNumber");
                                    output = @"d:\xml\output\" + custNumber;

                                    File.WriteAllText(output, el.ToString());
                                }                                    
                            }
                            break;
                    }
                }
            }

然后我使用 PowerShell 解析生成的文件,主要是因为我发现在服务器上使用更容易,同时规范可以更改,我可以即时更改脚本.

I then parse the resulting files with PowerShell, basically because I find it easier to work with on the server while specs can change and I can on the fly change the script.

那么……将上述内容也转换为 PowerShell 的最简单方法是什么,将 [.Net here] 放在一切之前?如果它在一行上有 "<cust" 而在下一行有 "omer>" ,我是否必须逐字节读取?

So... what is the easiest way to convert the above to PowerShell also, putting [.Net here] before everything ? would I have to read byte for byte just in the case it has "<cust" on one line and "omer>" on the next?

推荐答案

这应该非常接近您想要在 Powershell 中执行的操作:

This should be pretty close to what you wanted to do in Powershell:

$f = [System.Xml.XmlReader]::create("d:\xml\test.xml")

while ($f.read())
{
    switch ($f.NodeType)
    {
        ([System.Xml.XmlNodeType]::Element) # Make sure to put this between brackets
        {
            if ($f.Name -eq "Customer")
            {
                $e = [System.Xml.Linq.XElement]::ReadFrom($f)

                if ($e -ne $null)
                {
                    $custNumber = [string] $e.Element("CustNumber")

                    $e.ToString() | Out-File -Append -FilePath ("d:\xml\output\"+$e.ToString())
                }
            }
            break
        }
    }
}

这篇关于如何在 PowerShell 中使用 XmlReader 来流式传输大/巨大的 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆