tags:

views:

45

answers:

2

I have an xml node that looks like <slot highcount="20" lowcount="10" />

I've tried the following xpath expression:

XmlNode node = xdoc.SelectSingleNode("slot[@lowcount>=12] && slot[@highcount <=12]");

but I get an invalid token error and I don't have enough experience with this to know what I'm doing wrong. Any ideas?

+1  A: 

You need to use the "and" operator - there is no "&&" operator in XPath. I think something like this should work:

XmlNode node = xdoc.SelectSingleNode("slot[@lowcount>=12 and @highcount <=12]");

EDIT: This is the correct syntax, but to select the node specified in the question, we need to flip around the operators like the second snippet below. I'm leaving the original code sample in for context, so that the comment thread makes sense:

XmlNode node = xdoc.SelectSingleNode("slot[@lowcount<=12 and @highcount >=12]");
Paddyslacker
Thanks. I tried also that but I get the exception - "Expression must evaluate to a node-set." There is a node that should be matched for my criteria so I'm not sure why I'm getting that error. If I take out the first comparison or the 2nd and have it only evaluate one or the other I get back a node. It's when I try to do the "and" part that I get the error.
geoff swartz
If I use the expression /slots/slot[@lowcount >= '12'] and /slots/slot[@highcount <= '12'] I get the expression error. If I use yours -/slots/slot[@lowcount >= '12' and @highcount <= '12'] I get back null. Again, I know I have a node that should match so I'm not sure why it's doing that.
geoff swartz
@geoff. Take out the apostrophe around the number 12. The answer posted does not have apostrophe's around the comparison values.
Chris Persichetti
The comparison above is the correct syntax, but if that node is the only node in the xml doc, then the select single node will never return a node, because lowcount is never higher than 12 and highcount is never less than 12. If you flip the comparison operators around, then you'll get a single node. I'll edit my sample to demonstrate
Paddyslacker
A: 

XPath doesn't use && and ||; it uses and and or.

3.6 Logical Expressions

A logical expression is either an and-expression or an or-expression. If a logical expression does not raise an error, its value is always one of the boolean values true or false.

 [8]      OrExpr  ::= AndExpr ( "or" AndExpr )*
 [9]      AndExpr ::= ComparisonExpr ( "and" ComparisonExpr )*

References

polygenelubricants