473,407 Members | 2,359 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,407 software developers and data experts.

Strange behaviour with identical XmlNode.SelectSingleNode(xpathQuery) queries

I have two queries that appear to be exactly the same, but one of them
returns null while the other one returns a valid result!
Can anyone provide an explanation to why this is so? Below is an nunit
test that exposes the problem. I have run the test under both the 1.0
and 1.1 framework with the same result.

[Test]
public void XPathBooks()
{
XmlDocument smallDoc = new XmlDocument();
smallDoc.LoadXml("<book><title>Bugs</title></book>");
XmlNode book1 = smallDoc.SelectSingleNode(@"book");
XmlNode title1 = book1.SelectSingleNode(@"/book/title");
Assert.IsNotNull(title1, "title1");

XmlDocument largeDoc = new XmlDocument();
largeDoc.LoadXml("<books><book><title>Bugs</title></book></books>");
XmlNode book2 = largeDoc.SelectSingleNode(@"books/book");
Assert.AreEqual(book1.OuterXml, book2.OuterXml);
XmlNode title2 = book2.SelectSingleNode(@"/book/title");
Assert.IsNotNull(title2, "title2"); // This test fails!!
}

Thanks,
Rune
Nov 12 '05 #1
4 4928
"Rune" <ru*****@yahoo.no> wrote in message news:f6**************************@posting.google.c om...
I have two queries that appear to be exactly the same, but one of them
returns null while the other one returns a valid result!
Can anyone provide an explanation to why this is so?


Rune,

The queries are the same, but the XML node tree you're querying
and the context in which you're querying it has changed.

The first query is "/book/title" against the following XML document,

<book>
<title> Bugs </title>
</book>

Where the context node for the XPath expression is <book>
(observe that <book>'s ParentNode is "#document"). The
XPath expression is absolute, so it starts at #document,
finds #document has child named <book>, and that <book>
has child named <title>.

The second query is "/book/title" against the following XML
document,

<books>
<book>
<title> Bugs </title>
</book>
</books>

The context node for the XPath expression is <book> (observe
that <book>'s ParentNode is "books" and not "#document"). The
XPath expression is absolute, so it begins at #document. It finds
that #document has no immediate child named "book" (it has one
named "books," but that doesn't match), therefore the query
returns null.

The XPath expression equivalent to the first expression when
the context node is <book> in the second document would be:

XmlNode title2 = book2.SelectSingleNode(@"title");

As a general practice when writing XPath expressions, try not
to spell out the context node explicitly. For instance, instead
of using the relative expression "../book/title" (which may not
produce a single node if <book> has siblings), just say "title".
Derek Harmon
Nov 12 '05 #2
"Rune" <ru*****@yahoo.no> wrote in message news:f6**************************@posting.google.c om...
I have two queries that appear to be exactly the same, but one of them
returns null while the other one returns a valid result!
Can anyone provide an explanation to why this is so?


Rune,

The queries are the same, but the XML node tree you're querying
and the context in which you're querying it has changed.

The first query is "/book/title" against the following XML document,

<book>
<title> Bugs </title>
</book>

Where the context node for the XPath expression is <book>
(observe that <book>'s ParentNode is "#document"). The
XPath expression is absolute, so it starts at #document,
finds #document has child named <book>, and that <book>
has child named <title>.

The second query is "/book/title" against the following XML
document,

<books>
<book>
<title> Bugs </title>
</book>
</books>

The context node for the XPath expression is <book> (observe
that <book>'s ParentNode is "books" and not "#document"). The
XPath expression is absolute, so it begins at #document. It finds
that #document has no immediate child named "book" (it has one
named "books," but that doesn't match), therefore the query
returns null.

The XPath expression equivalent to the first expression when
the context node is <book> in the second document would be:

XmlNode title2 = book2.SelectSingleNode(@"title");

As a general practice when writing XPath expressions, try not
to spell out the context node explicitly. For instance, instead
of using the relative expression "../book/title" (which may not
produce a single node if <book> has siblings), just say "title".
Derek Harmon
Nov 12 '05 #3
Of course :) Thanks a lot
Nov 12 '05 #4
Of course :) Thanks a lot
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ethel Aardvark | last post by:
I am running a 9.0.1 database on a W2K server and have come across some strange behaviour with a SQL query. I have a query which runs in a PL/SQL cursor which has several PL/SQL variables used to...
4
by: Ryan | last post by:
Bit of an obscure one here, so please bear with me. I have two copies of a database which should be identical. Both have a complex view which is identical. I can open the views and the data is as...
4
by: Paul | last post by:
I have run into a strange problem with a site I am working on. My SELECT queries work fine, but I just tried to add an UPDATE statement and it fails. A test showed that INSERT fails also. I created...
0
by: Rune | last post by:
I have two queries that appear to be exactly the same, but one of them returns null while the other one returns a valid result! Can anyone provide an explanation to why this is so? Below is an...
4
by: John Smith | last post by:
Isn't life a bitch! You know what you want but you don't know how to get it. I have produced 12 queries that calculate a payment profile over 12 months. For a number of the records (ie with...
1
by: loosecannon_1 | last post by:
Hello everyone, I am hoping someone can help me with this problem. I will say up front that I am not a SQL Server DBA, I am a developer. I have an application that sends about 25 simultaneous...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
5
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed...
2
by: Claudio | last post by:
Hi I'm trying to use XPath queries with streaming XML, but I cannot make it working. The solution I'm trying to implement is: create a XmlNode as soon as I have a full XML element and use the...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.