如何使用XPath查询获取日期范围内的数据

时间:2013-01-21 10:13:40

标签: c# xml xpath

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<AppXmlLogWritter>    
  <LogData>
    <LogID>5678201301211226306349434819061642515658</LogID>
    <LogDateTime>**20130121**122630</LogDateTime>
    <LogType>Warning</LogType>
    <LogFlag>PMLogFlag</LogFlag>
    <LogApplication>PMLogApplication</LogApplication>
    <LogModule>PMLogModule</LogModule>
    <LogLocation>PMLogLocation</LogLocation>
    <LogText>PMLogText</LogText>
  </LogData>
  <LogData>
    <LogID>5678201301211226306349434819063542617610</LogID>
    <LogDateTime>**20130121**122630</LogDateTime>
    <LogType>Warning</LogType>
    <LogFlag>PMLogFlag</LogFlag>
    <LogApplication>PMLogApplication</LogApplication>
    <LogModule>PMLogModule</LogModule>
    <LogLocation>PMLogLocation</LogLocation>
    <LogText>PMLogText</LogText>
  </LogData>
  <LogData>
    <LogID>5678201301211226306349434819065542731198</LogID>
    <LogDateTime>**20130121**122630</LogDateTime>
    <LogType>Warning</LogType>
    <LogFlag>PMLogFlag</LogFlag>
    <LogApplication>PMLogApplication</LogApplication>
    <LogModule>PMLogModule</LogModule>
    <LogLocation>PMLogLocation</LogLocation>
    <LogText>PMLogText</LogText>
  </LogData>
  <LogData>
    <LogID>5678201301211226306349434819067442849561</LogID>
    <LogDateTime>**20130117**122630</LogDateTime>
    <LogType>Warning</LogType>
    <LogFlag>PMLogFlag</LogFlag>
    <LogApplication>PMLogApplication</LogApplication>
    <LogModule>PMLogModule</LogModule>
    <LogLocation>PMLogLocation</LogLocation>
    <LogText>PMLogText</LogText>
  </LogData>
</AppXmlLogWritter>

从上面的XmL文件中我想读取范围之间的xml文档。这样我使用datetimepicker控件为From Date和To Date实现了一个windows表单,当我从FROM DATE选择日期时,还有一个提交按钮,从TO DATE开始,它将日期显示在列表视图中,从这两个日期开始 为此,我已将代码编写为

private void btnSubmit_Click(object sender, EventArgs e)
        { 
 XmlElement objXmlRoot = objXmldoc.DocumentElement;
if (chkBoxFromDate.Checked && chkBoxToDate.Checked)
            {
                strXpathQuery = "/AppXmlLogWritter/LogData[LogDateTime >='" +  dateTimePickerFromDate.Value.ToString("yyyyMMdd") + "' and LogDateTime <='" + dateTimePickerToDate.Value.ToString("yyyyMMdd") + "']";
}
} 

我使用此查询显示数据,但它显示空白数据。

1 个答案:

答案 0 :(得分:0)

我有一种似曾相识的感觉。我不是answer this question for you吗?

strXpathQuery = "/AppXmlLogWritter/LogData[substring(LogDateTime, 1, 8) > " + 
    dateTimePickerFromDate.Value.ToString("yyyyMMdd") + 
    " and substring(LogDateTime, 1, 8) <= " + 
    dateTimePickerToDate.Value.ToString("yyyyMMdd") + "]";

或者格式化得更好

strXpathQuery = 
   string.Format("/AppXmlLogWritter/LogData[substring(LogDateTime, 1, 8) > {0:yyyyMMdd} and substring(LogDateTime, 1, 8) <= {1:yyyyMMdd}]", 
   dateTimePickerFromDate.Value, dateTimePickerToDate.Value);