0

我有下面的代码。<item></item>假设用户可以插入1-4个关键词并点击搜索按钮,那么如果子标签中的内容<description</description>包含一个/多个输入的关键词,就会有结果在richtextbox中显示出来。但是代码在这一行不正确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 回答