XmlReader help

I use the XmlReader class to read in large XML files because, unlike XmlDocument, the class reads the file in chunks.

Is there any way in XmlReader.StartElement to also search for child nodes of the current node, similar to XmlDocument.XQL? Unfortunately in XmlReader.StartElement only the name of the node and its attributes are passed, not the XmlNode.

I need this possibility to be able to imitate the correct object in the memory on the basis of the node, because there can be for example several objects which look like this: <row type="1"/>, however there are subnodes which still specify the type of the “Row”.


XMLreader is a forward only no look ahead data stream

What you could do is just accumulate the relevant data until you get to the matching end element and only then create the item with all the data you read
As you noted when you start an element there may not be enough information to do things correctly

1 Like

That was my idea anyway. Of course, this makes things a lot more complicated, since I have a few such nodes.

a stack of nodes works well for this :slight_smile:
push a new one, fill in the details as you go, when you get to the end of the element , pop it off the stack and save it somewhere else for whatever youre doing
the top of the stack is always the current item in progress

What do you mean? Create an XmlDocument from a temporary String (copy of a Row Node) that starts at (XmlReader.StartElement) and ends at (XmlReader.EndElement)? Would be a possibility, although then I would have to parse the individual subnodes of a row with XmlDocument again…

Or is your suggestion meant differently?

I assume you’re reading the xml and turning that into some internal data structure(not XML) ?

1 Like

Yes, that’s the point :wink:

there are a few ways to handle this
One might be

  • make a “stack” - really just an array of variants. we’ll ALWAYS operate on stack(stack.ubound) - the LAST element in the array
  • when the reader gets a start element ( or start document ) then you add a new instance of your custom class to the end of the stack
  • as you read the data you add properties
  • when you get another start element you add another item to the end of the array

It shard to give general advice as HOW you do this will vary somewhat depending on your classes and what you intend to do