0

私のプロジェクトのアイデアは、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)では、「Operator> =は文字列とテキストボックスに適用できません」と書かれているため、明らかに非常に間違っています。LINQ to XMLを使用すると作業が楽になることはわかっていますが、XmlDocumentを使い続けたい場合は、誰かが現在の問題を修正してくれませんか?私はプログラミングに非常に慣れていないので、XMLファイルの解析についてほとんど何も学びませんでした。

私のC#WinFormには、との2つのフィルターがtxtComStartDateありtxtComEndDateます。ユーザーは入力txtComStartDateまたはtxtComEndDate両方を入力できます。

ケース1:txtComStartDate-06/08/12の場合、結果は06/08/12から最新のものまでの私のrichComResults唯一の結果に表示されます。<item></item>


ケース2:txtComEndDate-07/08/12の場合、結果は07/08/12より前に表示されるものrichComResultsだけに表示され<item></item>ます。


ケース3:txtComStartDate-06/08/12&-07/08/ txtComEndDate12の場合、結果はこれら2つの日付内に発生したものrichComResultsだけに表示されます。<item></item>

4

1 に答える 1

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を参照してください。

于 2012-08-09T16:16:14.680 に答える