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

本文关键字:范围内 数据 取日期 获取 何使用 XPath 查询 | 更新日期: 2023-09-27 17:54:57

<?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文档。这样我实现了windows窗体的日期时间选择器控件从日期和到日期和一个提交按钮,当我选择日期从日期,也从日期到日期它显示日期到列表视图从这些从两个不同的日期为此,我将代码编写为

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") + "']";
}
} 

我使用这个查询来显示数据,但是它显示的是空白数据。

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

我有一种郁闷的感觉。我刚才不是帮你回答了这个问题吗?

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);