469,623 Members | 1,800 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

XPathNodeIterator.Count Performance Issues

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

Aug 8 '06 #1
2 3569
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. (sd*******@mailnull.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" <vo***********@gmail.comwrote in message
news:11**********************@n13g2000cwa.googlegr oups.com...
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

Aug 8 '06 #2

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

Aug 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Rolando Barberis | last post: by
1 post views Thread by csomberg | last post: by
2 posts views Thread by avnrao via .NET 247 | last post: by
4 posts views Thread by Jamie da Silva | last post: by
4 posts views Thread by Matthew Groch | last post: by
3 posts views Thread by KJ | last post: by
2 posts views Thread by R. Todd | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.