473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 2740
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vikas Vijay | last post by:
hello all, I have a c++ code in which is sth like while(i < N) { .... do some processing in c++ to create file abc system("simulator_name < abc") ......c++ ... processing...... }
0
by: Steve Taylor | last post by:
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...
4
by: gajba | last post by:
Hi, from an XML beginner.. I have an XML like the one below: <root> <vendor name="Microsoft"> <!-- additional sub-tags and items --> </vendor> <vendor name="Sun"> <!-- additional sub-tags...
4
by: viditmittal | last post by:
hi, i have a xml file <root> <Type /> <Description /> <Category /> <employee > <id>3</id>
0
by: Philipp Schumann | last post by:
Hi there, don't know whether anyone ran into this issue formerly (in .NET 1.0). I have an XML document with a default namespace (indicating the target namespace of the XML Schema used to...
0
by: LockyBoy | last post by:
Hi Have created an xml document with an xmltextwriter with a namespace, so it can be validated against an xsd schema. Later in my application, I want to extract values from the xml and am using...
12
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
16
by: Randy Harris | last post by:
I was inspired by the recent discussion of returning multiple recordsets to ADO from a stored procedure. (Amazed is probably more accurate). I asked about how to accomplish same with Oracle and...
2
by: darrel | last post by:
I have an XML file that looks like this: <Counties> <County> <District>2</District> <CountyName>Palm</CountyName> </County> <County> <District>3</District> <CountyName>McCarthy</CountyName>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.