tags:

views:

54

answers:

1

I am using .NET XmlReader because the xml file is way too big to use a DOM or other in-memory class, but I need to be able to somehow walk back up the xml hierarchy from the current position in XmlReader. Is there a way to do that? I sometimes need to walk back up to the level one parent to retrieve content. I could cache some xml as I parse, but these are big xml files so I dont want to store a lot of data as I parse through the xml as that could potentially exceed memory, or start slowing the application as it adds data to a growing memory structure.But maybe that is my only recourse.

Is there an xml parser, in any language, that can handle this?

+1  A: 

It sounds like you should be able to cache just a stack of current elements... I doubt that your XML file has deep enough nesting for that to become a problem. Every time you read the start of an element, push it onto the stack. Every time you read the end of an element, pop it off the stack.

You haven't really told us what information you need from the ancestor nodes, but so long as you only need the ancestor nodes - rather than siblings etc - that should work reasonably easily.

Be careful of self-closing elements such as <foo attr="value" /> - you need to push those onto the stack, do any necessary processing, and then pop them again even though you won't see a separate "end element" event.

Jon Skeet