469,268 Members | 1,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 2552
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.