xml - PowerShell 在读取大型 (50 MB) XML 文档时抛出 System.OutOfMemoryException

标签 xml powershell memory-management large-files

我们正在运行以下脚本:

[xml]$products = 获取内容 C:\fso\products.xml

并收到以下错误:

System.OutOfMemoryException

我们假设这是因为 XML 文件很大。解决方案可能涉及一次读取 XML 一行。 我们如何处理这个文件?例如,我们如何计算元素的数量?或者,我们如何将元素名称打印到控制台窗口?

我们目前正在查看此链接:

http://blogs.technet.com/b/stephap/archive/2009/05/27/choking-on-very-large-xml-files.aspx

XML结构如下:

<?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="Products.xsd" generated="2014-01-21T08:21:41">
        <Products>
            <upc>0000000000001</upc>
            <description>BASICS $1.00</description>
            <cost>0.6</cost>
            <normal_price>1</normal_price>
            <pricemethod>0</pricemethod>
            <target_margin>0</target_margin>
            <department>34</department>
            <pack>1</pack>
            <tax>3</tax>
            <foodstamp>0</foodstamp>
            <scale>0</scale>
            <dsd>0</dsd>
            <modified>2014-01-04T10:23:55</modified>
            <cost_modified>2012-11-11T11:20:58</cost_modified>
            <active>1</active>
            <advertised>0</advertised>
            <whomodified>170</whomodified>
            <longdescription>TEAR ISSUE</longdescription>
            <seconddescription>ROLL START</seconddescription>
            <discount>1</discount>
            <wicable>0</wicable>
            <validage>0</validage>
            <deleted>0</deleted>
            <attributes>2056</attributes>
            <Created>2005-02-16T09:53:00</Created>
            <CreatedBy>1</CreatedBy>
            <Points>0</Points>
        </Products>
        <Products>
            <upc>0000000000357</upc>
            <description>CHARMIN BATHROOM TISSUE</description>
            <cost>5.81</cost>
            <normal_price>7.99</normal_price>
            <pricemethod>0</pricemethod>
            <target_margin>0</target_margin>
            <department>4</department>
            <pack>1</pack>
            <size>OVERLIMIT</size>
            <tax>2</tax>
            <foodstamp>0</foodstamp>
            <scale>0</scale>
            <dsd>0</dsd>
            <modified>2010-06-30T23:55:00</modified>
            <active>0</active>
            <advertised>0</advertised>
            <whomodified>30</whomodified>
            <longdescription>CHARMIN BATHROOM TISSUE</longdescription>
            <discount>1</discount>
            <wicable>0</wicable>
            <validage>0</validage>
            <deleted>0</deleted>
            <attributes>2048</attributes>
            <Created>2005-02-16T09:53:00</Created>
            <CreatedBy>1</CreatedBy>
            <Points>0</Points>
        </Products>

最佳答案

最好使用 XPath 来查询此类文档。 XPath 通常可以在不需要将整个文档加载到 DOM 树中的流模式下工作。

参见Select-Xml:

以下将计算 XML 文件中的所有元素:

Select-Xml -Path C:\fso\products.xml -Xpath "count(//*)"

通过这种方式,您可以获取您要查找的 XML 的小片段或对其进行计算。

参见:http://technet.microsoft.com/en-us/library/hh849968.aspx

关于xml - PowerShell 在读取大型 (50 MB) XML 文档时抛出 System.OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21264411/

相关文章:

c - c中的不可变字符串的内存分配在哪里?

java - 当 JAXB 编码(marshal)可匹配两种类型的 XML 时,JAXB 如何决定创建哪种类型?

json - 将带有空格的参数传递到AWS cli

powershell - DHCP 保留删除脚本

Powershell 列出 Azure 存储上的所有表

c++ - mmap 与使用 new 分配的内存

ruby-on-rails - 哈希 xml 解析为 json 显示没有有效的根

java - 来自 wsdl 的 jaxb DTO 的自定义包

android - 检查是否已创建私有(private)偏好?

objective-c - 读取属性时CoreData泄漏