views:

172

answers:

2

Hi,

i have one performance problem with XML and ListView:

i have XML file with about 12000 nodes (yes, it it very much, but all nodes are necessary). This file has the following structure:

<?xml .... ?>
<MyRootNode>
<node name="name1" lang="en" artist="aaa" genre="dsdsds" dsadasd="dsdsd" />
...
<node name="name12000" lang="en" artist="aaa" genre="dsdsds" dsadasd="dsdsd" />
</MyRootNode>

and then i need load this document into ListView:

XmlDocument Doc = new XmlDocument();
Doc.Load("MyDoc.xml");

string[] SubItems = new string[4];
foreach(XmlNode Node in Doc.DocumentElement.ChildNodes) 
{
SubItems[0] = Node.Attributes["lang"].Value;
SubItems[1] = Node.Attributes["artist"].Value;
SubItems[2] = Node.Attributes["genre"].Value;
SubItems[3] = Node.Attributes["dsadasd"].Value
MyListView.Items.Add(Node.Attributes["Name"].Value).SubItems.Add(SubItems);
}

This process takes about 10 seconds and it is too long. Are there any ways to improve performance of such operation? I've tried to use Microsoft Parallel Extensions July 2008 CTP, but it didn't impoved anything, maybe because this operation can not be splitted into 2 separated threads. And where is the largest performance issue in this code?

A: 

Noticed that you're not using an XmlTextReader?

Might be worth checking that out. Don't know if it's going to be quicker, but worth a shot seeing as reading XML is it's main purpose.

Cheers, Sean

seanxe
+1  A: 

I would strong suspect that the XML is not the slow part of the code. Comment out the MyListView.Items.Add line and I suspect your XMLNode loop will execute in less than one second.

For a .NET ListView on a mid-range machine, building 1000 rows per second is a reasonable estimate of performance. This matches almost perfectly to your code's performance. You can try a couple of tricks to make it 10-20% faster, but to make it much faster, you will have to use a virtual ListView.

I have program LyricsFetcher which reads iTunes library XML file and builds a list of songs (which looks similar to what you are doing). This program uses the FastObjectListView from ObjectListView (an open source wrapper around .NET WinForms ListView). That program loads and displays 5000 songs in less than 1 second -- which I guess is the sort of performance you are looking for.

If you don't want to mess with a virtual listview, these are some performance tricks with a listview:

  • Bracket all updates between a BeginUpdate()/EndUpdate() pair
  • Remember to clear ListViewItemSorter before inserting new rows
  • Build an array of ListViewItems and then add them all at once with listView.Items.AddRange()

But using a virtual list is really the only way to significantly improve the performance.

Grammarian