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

C#Winforms DateTime-过滤XML文件中的日期,c#,xml,winforms,datetime,C#,Xml,Winforms,Datetime,我的项目想法是创建过滤器,这样我就可以过滤出我希望XML文件中出现的数据。我面临着日期时间不一致的问题。我的过滤器当前正在使用文本框。我希望使用DateTimePicker,但我不知道如何使用它。这是我第一次尝试编程和C。基本上,只要标记中的部分数据符合标准(即过滤器),那么整个将显示在我的RichTextBox结果区域中。目前,我陷入了需要处理datetime格式的困境。我完全迷路了 我的部分XML文件: <item> <title>[alfista] Max<

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

我的部分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>
在这一行
中,如果(itemDate>=txtComStartDate)
,它显然是非常错误的,因为它说“运算符>=不能应用于字符串和文本框”。我知道LINQtoXML可以让我的生活更轻松,但如果我想坚持使用XmlDocument,有人能帮我解决当前的问题吗?因为我对编程非常陌生,所以我对解析XML文件了解很少

我的C#winforms中有两个过滤器,分别是
txtComStartDate
txtComEndDate
。用户可以输入
txtComStartDate
txtComEndDate
或两者兼而有之

案例1:如果
txtComStartDate
-06/08/12,则结果将显示在my
richComResults
中,仅显示从06/08/12开始到最晚的


案例2:如果
txtComEndDate
-07/08/12,则结果将仅显示在我的
richComResults
中2012年07月08日之前出现的



案例3:如果
txtComStartDate
-06/08/12&
txtComEndDate
-07/08/12,则结果将显示在我的
richComResults
中,仅显示在这两个日期内发生的

您需要解析xml和文本框值中的日期字符串。看起来您的xml日期是
RFC1123
格式,因此
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
。有关日期时间格式字符串的更多阅读信息,请参见:

是的,我指示用户键入“DD/MM/YY”。我试过你提到的代码,它是有效的但有一件事我发现了,我试着把“08/08/12”作为开始日期,结果中出现了
周二,2012年8月7日23:34:11 GMT
。这和时区有关吗?也许吧。您可以调试并查看DateTime.Parse(“周二,2012年8月7日23:34:11 GMT”)为您提供了什么。我猜你会得到比8/8/12 00:00更大的东西。
using System.Globalization;

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