Grackel1234
Grackel1234

Reputation: 23

Why does this XPath filter not work (maximum number)?

<test>
   <child id="13680621263370126043"/>
   <child id="13680621263370124329"/>
</test>

Code:

doc.SelectNodes(@"/test/child[@Id=13680621263370126043]");

the returned list has both child nodes in it, what gives?

Upvotes: 2

Views: 50

Answers (2)

kjhughes
kjhughes

Reputation: 111726

Change

 /test/child[@Id=13680621263370126043]

to

 /test/child[@id='13680621263370126043']

because

  1. XML is case-sensitive.
  2. The value being tested is too long to be tested as a number.

Note on maximum numbers in XPath

XPath uses double-precision 64-bit format IEEE 754 value for numbers, which has 15-17 signicant decimal digits. The @id attribute here has 20 digits and therefore must be tested as a string, not as a number.

Upvotes: 2

Gilad Green
Gilad Green

Reputation: 37281

You have 2 problems:

  1. It is case sensitive. It should be id and not Id.
  2. Wrap the value with ''

So:

var doc = new XmlDocument();
doc.Load("data.xml");
var result = doc.SelectNodes(@"//test/child[@id='13680621263370126043']");

// result contains 1 item

Upvotes: 2

Related Questions