A quick way to get the number of elements in an XML document

Is there a best practice for getting the number of elements from an XML document for the purpose of a progress report? I have a 2 GB XML file containing flights that I need to process, and my idea is to first get the number of all elements in the file, and then use the counter to display xx flights that are imported into our database.

To process files, we use XmlTextReader in .NET (C #) to get data without reading the entire document in memory (similar to the syntax formatting of sax).

So the question is how can I quickly get the number of these elements ... is there any best practice or should I go through the whole document first and do something like I ++;

Thanks!

+4
source share
2 answers

Of course, you can just read the document twice - once, just to count the elements (continue to use XmlReader.ReadToFollowing , for example (or perhaps ReadToNextSibling ) increasing the counter when you go:

 int count = 0; while (reader.ReadToFollowing(name)) { count++; } 

However, this means reading the file twice ...

An alternative is to search for the length of the file, and when you read one file once, report the percentage of the file processed so far, depending on the position of the underlying stream. It will be less accurate, but much more effective. You will need to create an XmlReader directly from Stream so that you can continue to check the position.

+7
source
 int count = 0; using (XmlReader xmlReader = new XmlTextReader(new StringReader(text))) { while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.Equals("Flight")) count++; } } 
+1
source

Source: https://habr.com/ru/post/1440992/


All Articles