Suppose I have an XML looking like the following:
<Node1>
<ChildNd>
<GrandChildNd>
<a />
<b />
</GrandChildNd>
...
<GrandChildNd>
<b />
<c />
</GrandChildNd>
</ChildNd>
...
</ChildNd>
</Node1>
...
<NodeN>
In other words, much like most other XMLs, very similar structure between nodes and some repeated attributes / elements within them.
And, since most of my XMLs are > 200MBs, I'm working on creating my own parser using an XMLReader rather than the simpler models of XPath / Linq To XML.
In writing this parser, I've found that I rely very heavily on XMLReader.ReadSubTree
to ensure I am always staying within my desired node and knowing that when I close it, I'm at the end of the node I was currently parsing.
So, for example, suppose I wanted to loop through all the <GrandChildNd>
s in a particular <ChildNd>
, I've coded it something like this:
Using reader As XmlReader = XmlReader.Create(uri)
reader.ReadToFollowing("Node1")
reader.ReadToDescendant("ChildNd")
reader.ReadStartElement("ChildNd")
' Loop through all the <GrandChildNd>s
Do Until reader.NodeType = XmlNodeType.EndElement
Using GrandChildNdRdr As XmlReader = reader.ReadSubtree
ParseGrandChild(GrandChildNdRdr)
End Using
' Exit current <GrandChildNd>
reader.ReadEndElement()
Loop
End Using
And even within my ParseGrandChild
method, I use even more ReadSubTree
calls since I find that it ensures me that I won't read anything outside of that current node and when I close that sub-reader, it places me at the end tag of the node I was consuming.
From what I've read online, it seems that the ReadSubTree
method is fairly light and not bad to use, but I'm just wondering if, aside from going the XPath / Linq to XML route, there is a batter way to do this / I'm just doing things dead wrong.
This is still all very new to me, so any links / examples would be greatly appreciated!!
Also, I know this sample code was written in VB.NET, but I'm equally comfortable with C# / VB.NET solutions.
Thanks!!