Tutorial :XML reading optimization .NET



Question:

Here's what I've got thus far:

Dim xpDoc As New XPathDocument(strXmlUrl & strXmlInfo(0) & "?xml=1")  Dim xpNav As XPathNavigator = xpDoc.CreateNavigator()  Dim xpExpression(9) As XPathExpression  Dim xpIter(9) As XPathNodeIterator    xpExpression(0) = xpNav.Compile("/profile/steamID64")  'etc.. up to 9    For i = 0 To 9      xpIter(i) = xpNav.[Select](xpExpression(i))      While xpIter(i).MoveNext()          If xpIter(i).Count <> 0 Then              strXmlInfo(i) = xpIter(i).Current.Value          Else              strXmlInfo(i) = ""          End If      End While  Next  

These are the xml files that I'm parsing: http://steamcommunity.com/id/brosephus?xml=1

Any way of handling this that would perform significantly better?


Solution:1

If you want to squeeze performance, I'm suggesting XmlReader. It's forward-only though and of course a bit bulkier to use, you don't get any shiny XPath expressions or anything. I can't give any performance comparisons though, but it is seldom a bottleneck for me. I'm using it a lot for XML processing at work and it has never been a problem.

However there is the usual tradeoff between ease-of-use and performance of course. Maybe you should implement a version using the XmlReader and time them, it should give you a hint, if the difference is small enough you might as well stick with XPathNavigator as it's easier to use. With XmlReader there is usually additional state to be maintained, depending on the complexity of the XML.


Solution:2

I prefer using classes that parse the XML for me, like the DataSet class. It chokes on some very complex XML (says it's invalid) but it works fine on the example you provided.

I highlighted the code it takes to "parse" the XML (one line). The rest of this toy app is just building the navigation tree from the table names, then pointing the datagrid at the selected DataTable.

Source code for the app, or just an installer.


Solution:3

Yes, look into LINQ to XML. LINQ is especially powerful in dealing with relationships & querying & parsing through xml content.

LINQ


Solution:4

Short answer - I personally can't think of anything that will drastically improve the performance of that snippet.

Long answer - When you say perform significantly better, what is it exactly that you are trying to achieve? Xml is not a performant file format and so operations on xml will always be relatively slow. The only real way to get performance would be to try to minimise the amount of xml you are reading, for example:

  • Are you trying to do analysis of the data contained in those xml files?

If so, its likely that you will be reading the same data over and over again and so you are probably better off parsing all of the xml and putting it into a more suitable data source (such as an SQL database, or possibly even another database optimised for statistical analysis)

  • Are you only interested in a small section of the document (for example changes made since you last looked at the document)

If so then you should use an XmlReader - whereas an XmlDocument will load and parse the entire xml document, an XmlReader just reads as it goes - This means that the XmlReader is more performant when reading small sections of a large xml document.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »