Xpath计数和比较日期时间

时间:2011-11-14 16:43:02

标签: c# xml datetime xpath count

下面是我的xml ...

我想要做的是使用XPATH和计数表达式..我想要计算xml中的日期与今天日期的匹配。因此,例如今日日期是" 2011-05-05" ..与此日期匹配的任何消息都有一个计数并返回一个整数。所以答案是NumberofTodaysMessages = 2.

<Response>
    <run_id>1</run_id>
    <message>
      <timestamp>2011-05-05T10:50:00.46875+00:00</timestamp>
      <event_type>Information</event_type>
      <operation>LoadProjects</operation>
      <error_code />
      <details>LoadProjects request detected</details>
    </message>
    <message>
      <timestamp>2011-05-05T10:50:02.296875+00:00</timestamp>
      <event_type>Error</event_type>
      <operation>Processor.InitaliseDCFiles</operation>
      <error_code />
      <details>some error details</details>
    </message>
    <message>
      <timestamp>2011-11-10T10:50:02.296875+00:00</timestamp>
      <event_type>Debug</event_type>
      <operation>Processor.InitaliseDCFiles</operation>
      <error_code />
      <details>some details</details>
    </message>
  <Response> 

我将如何在XPATH中解决这个问题?

计数(/响应/消息/时间戳[@ DateTime.Now()])

2 个答案:

答案 0 :(得分:1)

xpath可以是:

today =“2011-11-05”//在您的日期库

的字符串中替换为“today”

xpath =“响应/消息/时间戳[starts-with(text()='”+ today +“')]”

答案 1 :(得分:0)

C#实现XPath 2.0,对吗?然后你应该拥有所有这些functions