473,382 Members | 1,336 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,382 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 2738
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>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.