By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,105 Members | 2,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,105 IT Pros & Developers. It's quick & easy.

XPathNodeIterator.Count Bug?

P: n/a
I couldn't find a better resource for reporting a possible .NET Framework 1.1
(SP0 & 1) bug, so if this is not the proper forum, please let me know.

The problem appears to occur when using "self::" to match the current node
of an XPathNavigator. In this case the resulting XPathNodeIterator returns a
Count of 1 even though it is properly empty as evidenced by MoveNext()
returning false.

In the following sample code (compiled as a console program), the results
from Test 2 & 10 are not correct.

static void Main(string[] args)
{
XPathDocument doc = new XPathDocument(new
StringReader("<root><a></a></root>"));
XPathNavigator nav = doc.CreateNavigator();

nav.MoveToFirstChild(); // now at "root"
Console.Out.WriteLine("Current Node Name: {0}\n",nav.Name);

RunSelectTest(nav,"self::root",1);
RunSelectTest(nav,"self::nomatch",2);

RunSelectTest(nav,"../root",3);
RunSelectTest(nav,"../nomatch",4);

RunSelectTest(nav,"a/self::a",5);
RunSelectTest(nav,"a/self::nomatch",6);

RunSelectTest(nav,"a",7);
RunSelectTest(nav,"nomatch",8);

nav.MoveToFirstChild(); // now at "a"

RunSelectTest(nav,"self::a",9);
RunSelectTest(nav,"self::nomatch",10);
}

static void RunSelectTest( XPathNavigator nav, string xpath, int testId )
{
Console.Out.WriteLine("--- Test {0} (\"{1}\") ---",testId,xpath);

XPathNodeIterator iter = nav.Select(xpath);
Console.Out.WriteLine("Select Iter Count: {0}",iter.Count);
Console.Out.WriteLine("Select Iter MoveNext: {0}",iter.MoveNext());
Console.Out.WriteLine(string.Empty);
}

Thanks,
Jamie da Silva
Silverlake Software LLC
Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jamie da Silva wrote:
XPathNodeIterator iter = nav.Select(xpath);
Console.Out.WriteLine("Select Iter Count: {0}",iter.Count);


I believe that's wrong. You must call MoveNext() before accessing any
properties.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #2

P: n/a
The documentation does not say that. As a matter of fact the Remarks for
Count warns that it is implemented by cloning and walking the nodes which
means the state of the Iterator should not matter. At a minimum that would
be a documentation problem.

However, if you reverse the output lines from RunSelectTest so that MoveNext
is called first you get the same results!

This still looks like an actual bug to me.

Jamie da Silva
Silverlake Software LLC

"Oleg Tkachenko" wrote:
Jamie da Silva wrote:
XPathNodeIterator iter = nav.Select(xpath);
Console.Out.WriteLine("Select Iter Count: {0}",iter.Count);


I believe that's wrong. You must call MoveNext() before accessing any
properties.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com

Nov 12 '05 #3

P: n/a
Hi Jamie,

You're right. The Count property on the iterator returns an incorrect result
for the said query. The upcoming Beta2 release for the next version of the
framework already has a fix for this issue.

Fyi, there is no need to invoke MoveNext() before calling Count on the
iterator.

Regards,
Ion

"Jamie da Silva" <Ja**********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
The documentation does not say that. As a matter of fact the Remarks for
Count warns that it is implemented by cloning and walking the nodes which
means the state of the Iterator should not matter. At a minimum that would be a documentation problem.

However, if you reverse the output lines from RunSelectTest so that MoveNext is called first you get the same results!

This still looks like an actual bug to me.

Jamie da Silva
Silverlake Software LLC

"Oleg Tkachenko" wrote:
Jamie da Silva wrote:
XPathNodeIterator iter = nav.Select(xpath);
Console.Out.WriteLine("Select Iter Count: {0}",iter.Count);


I believe that's wrong. You must call MoveNext() before accessing any
properties.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com

Nov 12 '05 #4

P: n/a
Thanks Ion. Fortunately it was pretty easy for me to workaround once i found
it.

Jamie da Silva
Silverlake Software LLC

"Ion Vasilian" wrote:
Hi Jamie,

You're right. The Count property on the iterator returns an incorrect result
for the said query. The upcoming Beta2 release for the next version of the
framework already has a fix for this issue.

Fyi, there is no need to invoke MoveNext() before calling Count on the
iterator.

Regards,
Ion

"Jamie da Silva" <Ja**********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
The documentation does not say that. As a matter of fact the Remarks for
Count warns that it is implemented by cloning and walking the nodes which
means the state of the Iterator should not matter. At a minimum that

would
be a documentation problem.

However, if you reverse the output lines from RunSelectTest so that

MoveNext
is called first you get the same results!

This still looks like an actual bug to me.

Jamie da Silva
Silverlake Software LLC

"Oleg Tkachenko" wrote:
Jamie da Silva wrote:

> XPathNodeIterator iter = nav.Select(xpath);
> Console.Out.WriteLine("Select Iter Count: {0}",iter.Count);

I believe that's wrong. You must call MoveNext() before accessing any
properties.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com


Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.