XmlReader - 如何处理大型 XML 文件? [英] XmlReader - how to deal with large XML-Files?
问题描述
我想知道如何在 XmlReader
中处理大而复杂的文件.就我而言,这是一个非常复杂的结构,我不能在单一方法中处理所有这些.在我看来这太脏了 - 所以我试图把这个方法分成几个更小的方法.
I was wondering how to deal with large and complex files in a XmlReader
.
In my case it's a pretty complex structure and I'm not okay with handling all of this inside a single-method. This is just too dirty in my opinion - so I tried to split the method into a few smaller methods.
因此我从以下代码开始:
Therefore I started with the following code:
// Create an XML reader for this file.
using (var reader = XmlReader.Create(new StringReader(fileText)))
{
while (reader.Read()){
if (reader.IsStartElement(){
switch (reader.Name){
case "FirstBigTag": MyMethod(reader, otherNecessaryMethods) break;
....
}
}
}
}
理论上这可以正常工作.但我经历过一些这种行为会导致错误状态的情况.
Theoretically this would work fine. But I experienced some cases where this behaviour results in a wrong state.
在某些情况下,MyMethod 正在解析正确的元素,然后我无法清楚地看到在哪里结束",因此我无法在不读取下一个节点的情况下离开该方法.这意味着我消耗了一个元素而不处理它(因为这应该由 main-method 处理).当我现在返回时,我又回到了我的主要"方法,但这是我的消耗字符串决定接下来必须调用哪个方法所必需的点.我该如何解决这个问题?有没有像reader.IsEndElement"或reader.look"这样的东西——不读取值但预见类型?
In some cases MyMethod is parsing the correct element and then I can't clearly see "where to end" so I can't leave the method without reading the next node. This would mean that I consume an element without handling it (because this is supposed to be handled by the main-method). When I now return I get back to my "main"-method but this is the point where my consumed-string would be necessary to decide which method has to be called next. How can I solve this? Is there anything like "reader.IsEndElement" or a "reader.look" - without reading the value but foreseeing the type?
推荐答案
如果文件太大而无法放入内存,您可以两全其美;使用 XmlReader
读取高级节点,然后使用 John Saunders 提到的高级抽象,即:
If the size of the file is too large to fit in memory, you could have the best of both worlds; read the high level nodes using XmlReader
, and then use the high level abstraction as John Saunders mentioned, ie:
case "FirstBigTag":
using(XmlReader subReader = reader.ReadSubtree())
{
XElement element = XElement.Load(subReader);
MyMethod(element);
}
break;
这篇关于XmlReader - 如何处理大型 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!