Performing sub select on XPathNavigator.Select() results

I have data that looks like this:

<Stats>
...
<Hits yyyy="2004" mm="02" dd="19" hh="00">
<FL>76</FL>
<HP>24</HP>
<EVNF>27</EVNF>
<EVS>53</EVS>
</Hits>
<Hits yyyy="2004" mm="02" dd="19" hh="01">
<FL>75</FL>
<HP>8</HP>
<CSINF>21</CSINF>
<CSIS>44</CSIS>
<EVNF>7</EVNF>
<EVS>21</EVS>
</Hits>
...
</Stats>
The <Hits /> node covers several months worth of data and will continue to grow. I need to be able to select a range of data within the document so I do the following:
XPathDocument doc = new XPathDocument(Server.MapPath("WebStats.xml"));
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator hits = nav.Select("//Hits[concat(@yyyy, @mm, @dd, @hh) >= '" + fromTimestamp + "' and concat(@yyyy, @mm, @dd, @hh) <= '" + thruTimestamp + "']");
Where fromTimestamp and thruTimestamp are values passed in from an aspx page. This works great to get the appropriate <Hits /> for the range that I need. However, I would like to be able to then perform additional "subqueries" on those results. For instance, I want to be able to get the totals (sum) of the various nodes under <Hits />. I know I could switch to using the Evaluate() method and have something along the lines of:
int aaHits = nav.Evaluate("sum(//Hits[concat(@yyyy, @mm, @dd, @hh) >= '" + fromTimestamp + "' and concat(@yyyy, @mm, @dd, @hh) <= '" + thruTimestamp + "']/AA)");
But to do that seems somewhat inefficient having to query the entire document every time for 6 - 8 different sub nodes. In addition, I place the hits object in cache because it is used from a handler that creates chart images based on the data. So ideally I'd like to select the nodes of the date range I need and avoid having to duplicate the logic for filtering in yet another page. I wouldn't mind duplicating the sub-query on another page since it should be simpler.
Thanks in advance for any assistance.
Matt
Matt Smith

Chief Swoof at swoofware.com
0
matt
2/20/2004 2:56:47 PM
asp.net.xml-datasource 7181 articles. 0 followers. Follow

2 Replies
188 Views

Similar Articles

[PageSpeed] 34

Yeah, bad idea. 

If nav is already positioned on appropriate Hits element, why do you need to search it again? Use relative XPath expressions (those don't start with /) - they are evaluated relatively to current node:
XPathNodeIterator hits = nav.Select("//Hits[concat(@yyyy, @mm, @dd, @hh) >= '" + fromTimestamp + "' and concat(@yyyy, @mm, @dd, @hh) <= '" + thruTimestamp + "']");
hits.MoveNext();
int aaHits = hits.Current.Evaluate("sum(AA)");

Oleg [XML MVP, MCPD]
XmlLab.Net | http://blog.tkachenko.com
0
olegt
2/22/2004 11:14:50 AM
double post
Oleg [XML MVP, MCPD]
XmlLab.Net | http://blog.tkachenko.com
0
olegt
2/22/2004 11:14:53 AM
Reply: