Avatar of cmhunty
cmhunty
 asked on

Returning a count from XPath

Hi

I simply want to return the count of a condition in an XML file using XPath.

My XPath expression works fine and have tested in XML Spy and it returns the right count but when I evaluate the expression, it always returns 0.

Dim xpd As New XPathDocument(strFileName)
Response.Write(fi.Name & "<br/>")
Dim xpn As XPathNavigator = xpd.CreateNavigator
Dim intCount As Int32 = xpn.Evaluate("count(Expression)")
Response.Write(fi.Name & "<br/>")

Only ever had to return a node set before. Am I going about returning a single value in the right way?

Any thoughts?

Chris
ASP.NETC#Visual Basic.NET

Avatar of undefined
Last Comment
cmhunty

8/22/2022 - Mon
Carl Tawn

If your expression returns a node-set then you may as well just return it into an XmlNodeList object and use the Count (or Length, can't remember which) property to get the number.
ASKER CERTIFIED SOLUTION
Bob Learned

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
cmhunty

ASKER
Thanks but still giving 0. I'm beginning to think it may be a problem with my XPath string even though it returns a proper count in XML Spy.
XPath:
count(//System/Payload/StaticEvent/ChargeCodeInst[contains(Code,'PHLX9994')])

TheLearnedOne - I'll give you the points as you've confirmed that I'm going about getting the result in the correct method. I think the problem must be somewhere else.

Cheers

Chris
Bob Learned

So, did you find a solution?

Bob
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
cmhunty

ASKER
Not yet, but I've found the problem.

The problem seems to be because the schema location is not valid. Wouldn't have thought this would be required for XPath query? No exceptions are thrown, XPath queries just don't work! Could this be a bug?

Anyway, back to finding a way of sorting this.............