C#Winforms DateTime - 过滤XML文件中的日期

时间:2012-08-09 16:02:39

标签: c# xml winforms datetime

我的项目想法是创建过滤器,以便我可以过滤出我希望XML文件中的数据出现的内容。我正面临着日期时间不一致的问题。我的过滤器目前正在使用TextBoxes。我希望使用DateTimePicker,但我不知道如何使用它。这是我第一次尝试编程和C#。基本上只要<item></item>标记中的部分数据符合条件(即过滤器),整个<item></item>就会显示在我的RichTextBox结果区域中。目前我陷入了这个需要处理日期时间格式的问题。我完全迷失了。

我的部分XML文件:

<item>
  <title>[alfista] Max</title>
  <author>alfista</author>
  <description>Or was it just populated by non spread betters, so you found it dull and boring??  See where I am coming from?  Puffy just posted general views about direction, and I much prefer them, but then I would wouldnt I.</description>
  <link>http://www.lse.co.uk/shareChat.asp?ShareTicker=BARC&amp;post=5657481</link>
  <pubDate>Tue, 08 Aug 2012 16:08:32 GMT</pubDate>
</item>
<item>
  <title>[Maximillian] F430</title>
  <author>Maximillian</author>
  <description>Ignore the snide comments and please  keep posting in the style you have been. This board was virtually dead until you came along a few weeks ago.  </description>
  <link>http://www.lse.co.uk/shareChat.asp?ShareTicker=BARC&amp;post=5657462</link>
  <pubDate>Tue, 07 Aug 2012 16:05:04 GMT</pubDate>
</item>
<item>
  <title>[colti] divi</title>
  <author>colti</author>
  <description>Does anyone know when the divi is actually paid please</description>
  <link>http://www.lse.co.uk/shareChat.asp?ShareTicker=BARC&amp;post=5658759</link>
  <pubDate>Wed, 06 Aug 2012 06:46:25 GMT</pubDate>
</item>
<item>
  <title>[SamSri] alfista</title>
  <author>SamSri</author>
  <description>Well, sea of knowledge is out there and thus there is always something new to learn. It's better for me to be humble.</description>
  <link>http://www.lse.co.uk/shareChat.asp?ShareTicker=BARC&amp;post=5659714</link>
  <pubDate>Wed, 05 Aug 2012 08:52:35 GMT</pubDate>
</item>

我的功能:

private void searchComByStartDate()
{
    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.

        string docPath = fileName;

        xmlDoc.Load(docPath); //* load the XML document from the specified file.

        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");

        foreach (XmlNode node in nodeList)
        {

            XmlElement itemElement = (XmlElement)node;

            string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;

            if (itemDate >= txtComStartDate)
            {
                string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
                string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;

                richComResults.AppendText("Author: " + itemAuthor + "\nDate: " + itemDate + "\nTitle: " + itemTitle + "\nDescription: " + itemDescription + "\n\n--------\n\n");
            }
        }
    }
}

在这一行if (itemDate >= txtComStartDate)中,显然非常错误,因为它说“运算符&gt; =无法应用于字符串和文本框”。我知道LINQ to XML可以让我的生活更轻松,但如果我想坚持使用XmlDocument,有人可以解决我当前的问题吗?因为我对编程很陌生,所以我只学习了解析XML文件的事情。

我的C#winforms中有两个过滤器,txtComStartDatetxtComEndDate。用户可以输入txtComStartDatetxtComEndDate或两者。

案例1:如果txtComStartDate - 06/08/12,则结果将显示在richComResults仅{/ 1}}从2012年8月6日开始到最晚的<item></item>


案例2:如果txtComEndDate - 2012年8月7日,则结果将仅显示在richComResults仅显示在2012年8月7日之前的<item></item>


案例3:if txtComStartDate - 06/08/12&amp; txtComEndDate - 2012年8月7日,结果将显示在我的richComResults中,只显示在这两个日期内发生的<item></item>

1 个答案:

答案 0 :(得分:0)

您需要在xml和textbox值中解析日期字符串。您的xml日期看起来是RFC 1123格式,因此DateTime.Parse(itemDate)应该有效。 至于文本框,这取决于用户,你不能确定他使用的格式是什么。您可以指示他以“dd / MM / yy”(这是您使用的)输入日期,并使用DateTime.ParseExact解析日期。

using System.Globalization;

CultureInfo provider = CultureInfo.InvariantCulture;
if (DateTime.Parse(itemDate) >= DateTime.ParseExact(txtComStartDate.Text, "dd/MM/yy", provider))

我建议您尝试使用DateTimePicker。有关DateTime格式字符串的更多阅读,请在此处查看: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx