HighTechTalks DotNet Forums  

XPathNodeIterator.Count Performance Issues

Dotnet XML microsoft.public.dotnet.xml


Discuss XPathNodeIterator.Count Performance Issues in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Volkan
 
Posts: n/a

Default XPathNodeIterator.Count Performance Issues - 08-08-2006 , 01:15 PM






Hi,

I'm trying to compare two XML documents and i'm using XPath queries to
select nodes. XPathNavigator's Select method runs fast enough and
returns an XPathNodeIterator object. When i try to access this
iterator's Count property the process extremely slows down. If i just
iterate throgh 18.000 nodes and call XPathNavigator.Select to find
equivalent node from the other document it doesn't even take 1 second.
But in the same loop, when i try to access XPathNodeIterator.Count, (by
accessing i mean just assigning its value to a variable) it takes about
5 minutes.

I tried to use XPathNavigator.Evaluate and used XPath function boolean
inside the expression. And this reduced the elapsed time down to 3
minutes but it's still too much for me. (Btw, using XPath's count
function took longer.)

Can someone please give me some advise about how to overcome this
problem? Any input is appreciated.

Thanks in advance.

Best regards,

Volkan


Reply With Quote
  #2  
Old   
Sergey Dubinets
 
Posts: n/a

Default Re: XPathNodeIterator.Count Performance Issues - 08-08-2006 , 02:49 PM






XPathNavigator.Select() doesn't evaluate the expression, just parses it.

XPathNodeIterator.MoveNext() does actual "lasy" evaluation and positiones
iterator to next node.
XPathNodeIterator.Count() called firs time clones entire expression and
evaluate it counting nodes. I expect that Clone() is expensive on big
node-sets.

I'd like to see the repro: XML file, XPath expression, how you create
XPathNodeIterator and how you use it. (sdub.xslt (AT) mailnull (DOT) com)

As work around you can try XPath function count(). It may be more efficient
then XPathNodeIterator.Count();

public virtual int Count {
get {

if (count == -1) {

XPathNodeIterator clone = this.Clone();

while(clone.MoveNext()) ;

count = clone.CurrentPosition;

}

return count;

}

}

Sergey

"Volkan" <volkan.paksoy (AT) gmail (DOT) com> wrote

Quote:
Hi,

I'm trying to compare two XML documents and i'm using XPath queries to
select nodes. XPathNavigator's Select method runs fast enough and
returns an XPathNodeIterator object. When i try to access this
iterator's Count property the process extremely slows down. If i just
iterate throgh 18.000 nodes and call XPathNavigator.Select to find
equivalent node from the other document it doesn't even take 1 second.
But in the same loop, when i try to access XPathNodeIterator.Count, (by
accessing i mean just assigning its value to a variable) it takes about
5 minutes.

I tried to use XPathNavigator.Evaluate and used XPath function boolean
inside the expression. And this reduced the elapsed time down to 3
minutes but it's still too much for me. (Btw, using XPath's count
function took longer.)

Can someone please give me some advise about how to overcome this
problem? Any input is appreciated.

Thanks in advance.

Best regards,

Volkan




Reply With Quote
  #3  
Old   
Volkan
 
Posts: n/a

Default Re: XPathNodeIterator.Count Performance Issues - 08-09-2006 , 01:29 AM




Hi Sergey,

Thanks you for your reply.

Below is how i do what i aim to do :

// Load files to XPathDocuments
XPathDocument pathDocOld = new XPathDocument(strOldFilePath);
XPathDocument pathDocNew = new XPathDocument(strNewFilePath);

// Create XPathNavigators
navOld = pathDocOld.CreateNavigator();
navNew = pathDocNew.CreateNavigator();

// Select main nodes from both documents
XPathNodeIterator iteratorOld =
navOld.Select("/Message/RestrictionList/*");
XPathNodeIterator iteratorNew =
navNew.Select("/Message/RestrictionList/*");

// Start looping with new document.
while (iteratorNew.MoveNext())
{
// For each item find the corresponding one in the old document
iteratorOldSub =
navOld.Select/Message/RestrictionList/RestrictedItem[@ItemId=\"" +
strItemID + "\"]");

if( iteratorOldSub.Count == 0 ) (1)
{
// Couldn't find the item in the old document so mark this one as
Inserted in the output.
}

}

As i mentioned earlier, it takes 1 sec with code block (1) and 5 mins
with it.

Then i tried :


bool bExists =
(bool)navOld.Evaluate("boolean(/Message/RestrictionList/RestrictedItem[@ItemId=\""
+ strItemID + "\"] )");

if( bExists )
{

}

This works twice faster but still not enough for me. I wonder what am i
doing wrong.

So any suggestions to improve the performance?

Thanks.

Volkan


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.