我的项目想法是创建过滤器,以便我可以过滤掉我希望 XML 文件中的数据出现的内容。我正面临日期时间不一致的问题。我的过滤器目前正在使用文本框。我希望使用 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&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&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&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&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)
中,显然是非常错误的,因为它说“运算符 >= 不能应用于字符串和文本框”。我知道 LINQ to XML 可以让我的生活更轻松,但如果我想坚持使用 XmlDocument,有人可以解决我当前的问题吗?因为我对编程很陌生,所以我对解析 XML 文件只学了很少的东西。
我的 C# winform 中有两个过滤器,它们是txtComStartDate
和txtComEndDate
. 用户可以输入txtComStartDate
或txtComEndDate
两者都输入。
案例 1:如果txtComStartDate
- 06/08/12,那么结果将显示在我richComResults
的仅从<item></item>
06/08/12 开始到最晚的那个。
情况 2:如果txtComEndDate
- 2012 年 7 月 8 日,那么结果将richComResults
只<item></item>
显示在 07/08/12 之前出现的那个。
案例 3:如果txtComStartDate
- 06/08/12 & txtComEndDate
- 07/08/12,那么结果将显示在我richComResults
仅<item></item>
在这两个日期内发生的情况。