472,146 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Reusing XPathExpression in multiple iterations

Is there some reason why I shouldn't be able to use the same XPathExpression
simultaneously in multiple iterations? The test below illustrates the
problem. It seems that creating two XPathNodeIterators with the same
expression creates a coupling between the iterators so they always point to
the same position. Anyone care to comment?

[Test]
public void ReuseCompiledExpr()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml( "<Top> <A> <A1/> <A2/> </A > <B/> </Top>" );

XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile( "*" );

nav.MoveToFirstChild(); // /Top
XPathNodeIterator iter1 = nav.Select( expr );
iter1.MoveNext(); // /Top/A
XPathNodeIterator iter2 = iter1.Current.Select( expr ); // Replace expr
with "*" and it works!
iter2.MoveNext(); // /Top/A/A1
iter2.MoveNext(); // /Top/A/A2
Assert.IsTrue( iter1.MoveNext() ); // /Top/B <--- Returns false!
}

---
Steve
Nov 12 '05 #1
2 2672
You have to call Clone explicitly: XPathNodeIterator iter2 =
iter1.Current.Select( expr.Clone() ); // Replace expr
Because there are no guaranties about concurrent usage of XPathExpression is
not really a bug, but a usability issue. We will consider fixing it in the
next version.
"Steve Taylor" <NO**********@SPAMhotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Is there some reason why I shouldn't be able to use the same
XPathExpression
simultaneously in multiple iterations? The test below illustrates the
problem. It seems that creating two XPathNodeIterators with the same
expression creates a coupling between the iterators so they always point
to
the same position. Anyone care to comment?

[Test]
public void ReuseCompiledExpr()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml( "<Top> <A> <A1/> <A2/> </A > <B/> </Top>" );

XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile( "*" );

nav.MoveToFirstChild(); // /Top
XPathNodeIterator iter1 = nav.Select( expr );
iter1.MoveNext(); // /Top/A
XPathNodeIterator iter2 = iter1.Current.Select( expr ); // Replace expr
with "*" and it works!
iter2.MoveNext(); // /Top/A/A1
iter2.MoveNext(); // /Top/A/A2
Assert.IsTrue( iter1.MoveNext() ); // /Top/B <--- Returns false!
}

---
Steve

Nov 12 '05 #2
You have to call Clone explicitly: XPathNodeIterator iter2 =
iter1.Current.Select( expr.Clone() ); // Replace expr
Because there are no guaranties about concurrent usage of XPathExpression is
not really a bug, but a usability issue. We will consider fixing it in the
next version.
"Steve Taylor" <NO**********@SPAMhotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Is there some reason why I shouldn't be able to use the same
XPathExpression
simultaneously in multiple iterations? The test below illustrates the
problem. It seems that creating two XPathNodeIterators with the same
expression creates a coupling between the iterators so they always point
to
the same position. Anyone care to comment?

[Test]
public void ReuseCompiledExpr()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml( "<Top> <A> <A1/> <A2/> </A > <B/> </Top>" );

XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile( "*" );

nav.MoveToFirstChild(); // /Top
XPathNodeIterator iter1 = nav.Select( expr );
iter1.MoveNext(); // /Top/A
XPathNodeIterator iter2 = iter1.Current.Select( expr ); // Replace expr
with "*" and it works!
iter2.MoveNext(); // /Top/A/A1
iter2.MoveNext(); // /Top/A/A2
Assert.IsTrue( iter1.MoveNext() ); // /Top/B <--- Returns false!
}

---
Steve

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Vikas Vijay | last post: by
reply views Thread by Steve Taylor | last post: by
4 posts views Thread by gajba | last post: by
4 posts views Thread by viditmittal | last post: by
reply views Thread by Philipp Schumann | last post: by
12 posts views Thread by Thomas Scheiderich | last post: by
16 posts views Thread by Randy Harris | last post: by
2 posts views Thread by darrel | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.