如何根据xpath中的日期范围选择节点? [英] How do I select nodes based on a date range in xpath?

查看:67
本文介绍了如何根据xpath中的日期范围选择节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML: http://www.lakelandcollege.ca/rustlers/all-news.xml

我使用以下代码选择了一年的物料节点:

I was selecting a year's worth of item nodes using this code:


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/rustlers/all-news.xml"));
DataListArchive.DataSource = xmlDoc.SelectNodes("/items/item[contains(date,'2011')]");
DataListArchive.DataBind();

但是现在我需要选择日期为2011年4月1日和2012年3月31日的项目节点.如何将XML中的日期转换为实际日期值并将其与范围进行比较?

But now I need to select item nodes where the date is btw 01-Apr-2011 and 31-Mar-2012. How do i convert the date in the XML to a real date value and compare it to a range?

我会喜欢:


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/rustlers/all-news.xml"));
DataListArchive.DataSource = xmlDoc.SelectNodes("/items/item[date between '01-APR-2011' and '31-MAR-2012']");
DataListArchive.DataBind();

推荐答案

在XPath 1.0中,没有通用的方法将字符串转换为日期,也没有操纵日期的方式-对于您的具体情况(检查日期是3月还是4月)特定年份):

There is no general way to convert string to date nor to manipulate dates in XPath 1.0 - for your specific case (checking if a date is in March or April of a specific year):

/items/item[(contains(date,'April') or contains(date,'March')) and contains(date,'2010')]

*更新*

看过代码,我会说最简单的方法是使用XPath获取所有 item 元素,然后使用C#进行过滤-即获取 date的值子元素,使用 DateTime.Parse 将其转换为日期时间,然后使用C#代码进行比较.

Having seen the code I'd say that the easiest way is to use an XPath to get all item elements and then do the filtering using C# - i.e. get the value of the date sub-element, convert it to a date-time using DateTime.Parse and then do the comparison in C# code.

可以将扩展功能添加到.NET XPath中以执行所需的操作(请参见

It is possible to add extension functions to the .NET XPath to do what you need (see this), but it is quite complex and probably not worth the effort in this case.

这篇关于如何根据xpath中的日期范围选择节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆