Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理非常大的XML文件_C#_C++_Xml_Large Files - Fatal编程技术网

C# 处理非常大的XML文件

C# 处理非常大的XML文件,c#,c++,xml,large-files,C#,C++,Xml,Large Files,我需要处理具有以下结构的XML文件: <FolderSizes> <Version></Version> <DateTime Un=""></DateTime> <Summary> <TotalSize Bytes=""></TotalSize> <TotalAllocated Bytes=""></TotalAllocated

我需要处理具有以下结构的XML文件:

<FolderSizes>
    <Version></Version>
    <DateTime Un=""></DateTime>
    <Summary>
        <TotalSize Bytes=""></TotalSize>
        <TotalAllocated Bytes=""></TotalAllocated>
        <TotalAvgFileSize Bytes=""></TotalAvgFileSize>
        <TotalFolders Un=""></TotalFolders>
        <TotalFiles Un=""></TotalFiles>
    </Summary>
    <DiskSpaceInfo>
        <Drive Type="" Total="" TotalBytes="" Free="" FreeBytes="" Used=""
               UsedBytes=""><![CDATA[ ]]></Drive>
    </DiskSpaceInfo>
    <Folder ScanState="">
        <FullPath Name=""><![CDATA[ ]]></FullPath>
        <Attribs Int=""></Attribs>
        <Size Bytes=""></Size>
        <Allocated Bytes=""></Allocated>
        <AvgFileSz Bytes=""></AvgFileSz>
        <Folders Un=""></Folders>
        <Files Un=""></Files>
        <Depth Un=""></Depth>
        <Created Un=""></Created>
        <Accessed Un=""></Accessed>
        <LastMod Un=""></LastMod>
        <CreatedCalc Un=""></CreatedCalc>
        <AccessedCalc Un=""></AccessedCalc>
        <LastModCalc Un=""></LastModCalc>
        <Perc><![CDATA[ ]]></Perc>
        <Owner><![CDATA[ ]]></Owner>

        <!-- Special element; see paragraph below -->
        <Folder></Folder>
    </Folder>
</FolderSizes>

仍在运行,27分钟:(

您可以将XML作为逻辑元素流读取,而不是尝试逐行读取,然后自己将其拼凑在一起


另外,您的问题已经被问到了

谢谢您的回答。我现在正在实现和测试它。其他人以前确实问过这个问题,但据我所知,他们中没有人处理11GB的文件。将它加载到内存中不起作用,逐行处理也不起作用。这是我的问题。代码我链接到应该逐个元素处理xml元素,而不将其全部加载到Ramai中。我实现了它,现在正在运行它。有关更多信息,请参阅更新的帖子。您实际上不是逐行阅读文件:您是逐节点解析文档。但很抱歉,我不明白为什么要花这么长时间(我对Microsoft.NET解析器不太熟悉;我对大型文档的大部分经验都是使用Java。)我认为对XML流式处理的合理预期是1Gb/分钟左右。当你说它“花了很长时间”时,您需要对此进行量化,以便我们了解您的期望是否合理。当然,您还需要增加对数据进行有用操作的成本,例如将数据加载到数据库中。感谢您的反馈Michael,请参阅更新的帖子以获取所需信息。这是否回答了您的问题?
    private int _totalLines = 0;
    private bool _cancel = false; // set to true when the cancel button is clicked

    private void CalculateFileSize()
    {
        xmlStream = new StreamReader(_filePath);
        xmlReader = new XmlTextReader(xmlStream);

        while (xmlReader.Read())
        {
            if (_cancel)
                return;

            if (xmlReader.LineNumber > _totalLines)
                _totalLines = xmlReader.LineNumber;

            InterThreadHelper.ChangeText(
                lblLinesRemaining, 
                string.Format("{0} lines", _totalLines));

            string elapsed = string.Format(
                "{0}:{1}:{2}:{3}",
                timer.Elapsed.Days.ToString().PadLeft(2, '0'),
                timer.Elapsed.Hours.ToString().PadLeft(2, '0'),
                timer.Elapsed.Minutes.ToString().PadLeft(2, '0'),
                timer.Elapsed.Seconds.ToString().PadLeft(2, '0'));

            InterThreadHelper.ChangeText(lblElapsed, elapsed);

            if (_cancel)
                return;
        }

        xmlStream.Dispose();
    }