问题
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.
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?
回答1:
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;
来源:https://stackoverflow.com/questions/24045243/xmlreader-how-to-deal-with-large-xml-files