0

以下のコードがあります。ユーザーが 1 ~ 4 個のキーワードを入力して検索ボタンをクリックすると<item></item>、サブタグのコンテンツに入力さ<description</description>れたキーワードが 1 つ以上含まれている場合、リッチテキスト ボックスに全体が表示される結果が得られます。しかし、この行のコードは正しくありませんif (itemDescription.Contains(txtComKeyword1 | txtComKeyword2 | txtComKeyword3 | txtComKeyword4)

よろしければ、ご覧いただけますか?あなたの助けは大歓迎です! ありがとうございました。

以下は、私の XML ファイル構造の一部です。

<item>
      <title>[PhoTosynthesIs] Being driven</title>
      <author>PhoTosynthesIs</author>
      <description>purely by profit, I've decided to stick to my strategy and exit both tranches at 177. Will pick this stock up again when it breaches and holds the next pivot point. gl all</description>
      <link>http://www.lse.co.uk/shareChat.asp?ShareTicker=BARC&amp;post=5660817</link>
      <pubDate>Wed, 08 Aug 2012 11:43:17 GMT</pubDate>
</item>
<item>
      <title>[b36m] alw51</title>
      <author>b36m</author>
      <description>Could you share your thoughts/opinions  on a buy in price based on TW with me please   many thanks</description>
      <link>http://www.lse.co.uk/shareChat.asp?ShareTicker=BARC&amp;post=5660636</link>
      <pubDate>Wed, 08 Aug 2012 11:16:56 GMT</pubDate>
</item>

以下は、この関数の私のコードです。

private void searchComByKeywords()
{
    // Process the list of files found in the directory. 
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
        try
        {
            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 itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;

                if (itemDescription.Contains(txtComKeyword1 | txtComKeyword2 | txtComKeyword3 | txtComKeyword4)
                {
                    string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
                    string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
                    string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;

                    richComResults.AppendText("Author: " + itemAuthor + "\nDate: " + itemDate + "\nTitle: " + itemTitle + "\nDescription: " + itemDescription + "\n\n--------\n\n");
                }
                //else
                //{
                //    richComResults.AppendText("There is no author " + txtComAuthor.Text.ToString().ToLower() + ". Please ensure you are using a correct author name.");
                //}
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
4

2 に答える 2

1

多分あなたは試すことができます:

if (itemDescription.Contains(txtComKeyword1) || itemDescription.Contains(txtComKeyword2) || itemDescription.Contains(txtComKeyword3) || itemDescription.Contains(txtComKeyword4))
{
...
}
于 2012-08-08T19:19:05.337 に答える
0

ステートメントの条件ifが複雑で、その意味がすぐにはわからない場合は、別のメソッドにリファクタリングする傾向があります。あなたの場合、次のようになります。

private static bool DoesItemDescriptionContainOneOf(string description, params string[] items)
{
    return items.Any(description.Contains);
}

次に、あなたの状態は次のようになります。

if (DoesItemDescriptionContainOneOf(itemDescription, txtComKeyword1, txtComKeyword2, txtComKeyword3, txtComKeyword4))
{
    ....
}

ずっときれいですね。

于 2012-08-08T19:25:43.500 に答える