views:

44

answers:

4

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?

+1  A: 

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

LINQ

SoftwareGeek
I was thinking more along the lines of XmlReader vs. XPathNavigator but I'll try this out as well to see if the performance gains are noticeable.
Radu
@Radu - yes, xmlreader is definitely quicker but linq makes it much simpler to parse/query your xml. For smaller files the difference is not much.
SoftwareGeek
I think I'll stick with xmlreader then, it's just that once I've got this code in, it probably won't need any maintenance so I'm not worried about how complicated this particular snippet will be.
Radu
+1  A: 

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.

Skurmedel
I'll follow your advice and try and time it. It's not that essential that the application performs a few fractions of a second faster...but, I want it to..
Radu
+1  A: 

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.

Kragen
I'm trying to grab some of that data out and display it. Most of the data will be changing on a semi-regular basis - it's basically information about a person and their status online.
Radu
Also, I used XmlDocument because I figured since I already need 10 pieces of data from that document, I might as well just load the whole thing and parse it directly instead of sequentially. I actually have something in XmlReader already so I'll give that a try.
Radu
+2  A: 

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.

Ron Savage
Wow that's pretty cool!
Radu