C# XPath

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use the following xpath to get nodes that comply with certain
criteria and have tested the xpath using XMLSpy. The xpath also works fine in
the vbscript that showed it as a sample. Using C# however, nothing is
returned since the "<" seems to be causing a problem. Visual Studio .NET 2003
seems to be using xpath 1.0, because using "le" works in XMLSpy but fails in
my code.

Is there any way I can modify the <= or do something to this xpath for it to
work with C#? I tried a @ in front of the variable holding the xpath and that
also failed. I have not been able to find anything anywhere for this case.
Please help.

string xpath = @"//Parent/Child[Completed=1 and Date <= """ + ClaimDate +
"""]";
 
try changing the < to a &lt; I would also verify that the resultant
Xpath query is what you expect it to be and that if there are any
namespaces on the xml nodes that you add a Namespace manager
appropriately.
 
try changing the < to a < I would also verify that the resultant
Xpath query is what you expect it to be and that if there are any
namespaces on the xml nodes that you add a Namespace manager
appropriately.
I had previously found information indicating that &llt; would work, but I
get an error that repeats my xpath and appends "has an invalid token", when I
try < or ≤. Just removing the "<", "<" or "≤" will work, but I need to do a
<=, not just an =.
 
String xq = String.Format(
"//downloadmodule[@Architecture=\"{0}\" and @Name=\"{4}\" " +
"and ( version/@Major>{1} or version/@Major={1} " +
" and (version/@Minor > {2} or version/@Minor = {2}" +
"and version/@Build > {3}))]",
Architecture, Major, Minor, Build, Name);
XmlElement nodeUpdate =
(XmlElement)xmlUpdateCfg["updateinfo"].SelectSingleNode(xq);

Works fine for me. If you have specific code that fails then please
a) provide code that is droppable into any project that exhibits this
behavior and often times specifying "how it fails" would also be
helpful.
 
LStatie said:
String xq = String.Format(
"//downloadmodule[@Architecture=\"{0}\" and @Name=\"{4}\" " +
"and ( version/@Major>{1} or version/@Major={1} " +
" and (version/@Minor > {2} or version/@Minor = {2}" +
"and version/@Build > {3}))]",
Architecture, Major, Minor, Build, Name);
XmlElement nodeUpdate =
(XmlElement)xmlUpdateCfg["updateinfo"].SelectSingleNode(xq);

Works fine for me. If you have specific code that fails then please
a) provide code that is droppable into any project that exhibits this
behavior and often times specifying "how it fails" would also be
helpful.
The code that fails is below:

String xPath = String.Format( "//transactions/transaction[Completed=1 and
(EffectiveDate < '{0}' and EffectiveDate = '{0}')][last()]", ClaimDate);

XmlElement root = doc.DocumentElement;
XmlElement node = (XmlElement) root.SelectSingleNode( xPath );

I tried \" instead of ' around {0}.
I tried XmlNode instead of XmlElement
I tried @"//transa..." instead of "//transa..."

What keeps happening is that as soon as there is a < or > in the xPath, the
SelectSingleNode returns null.

P.S. I know that the xml loaded fine and all else works, because all I have
to do is remove the < from the xPath and I get a node back.

Correction:
String xPath = String.Format( "//transactions/transaction[Completed=1 and
(EffectiveDate < '{0}' or EffectiveDate = '{0}')][last()]", ClaimDate);
 
Back
Top