当输出包含的一个或多个关键字为空时,不会筛选输出

本文关键字:输出 筛选 关键字 包含 一个 | 更新日期: 2024-11-08 10:03:10

下面的函数在此行无法正常工作

if (itemDescription.ToLower().Contains(txtComKeyword1.Text.ToLower()) ||
    itemDescription.ToLower().Contains(txtComKeyword2.Text.ToLower()) ||
    itemDescription.ToLower().Contains(txtComKeyword3.Text.ToLower()) ||
    itemDescription.ToLower().Contains(txtComKeyword4.Text.ToLower()))

如果一个或多个txtComKeyword为空。只有当所有四个txtComKeyword都填满时,它才能正常工作。

我想通过使用关键字限制 XML 文件的数据来过滤它。<item></item>中检测到哪些关键字将显示为结果。我想允许用户选择是否只输入一个/两个/三个/四个关键字。不幸的是,只要有空txtComKeywords,就会输出整个XML文件的<item></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.ToLower().Contains(txtComKeyword1.Text.ToLower()) || itemDescription.ToLower().Contains(txtComKeyword2.Text.ToLower()) || itemDescription.ToLower().Contains(txtComKeyword3.Text.ToLower()) || itemDescription.ToLower().Contains(txtComKeyword4.Text.ToLower()))
                {
                    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");
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

编辑:

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;
                string[] words = new String[] { null, null, null, null };
                string key1 = txtComKeyword1.Text.Trim();
                string key2 = txtComKeyword2.Text.Trim();
                string key3 = txtComKeyword3.Text.Trim();
                string key4 = txtComKeyword4.Text.Trim();
                words[0] = (key1.Length == 0 ? null : key1.ToLower());
                words[1] = (key2.Length == 0 ? null : key2.ToLower());
                words[2] = (key3.Length == 0 ? null : key3.ToLower());
                words[3] = (key4.Length == 0 ? null : key4.ToLower());
                if (words.Contains(itemDescription.ToLower())) 
                {
                    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");
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

当输出包含的一个或多个关键字为空时,不会筛选输出

在进入循环之前阅读所有单词并将它们保存在字符串数组
中在数组中输入单词之前,请检查文本框是否为空
然后使用 Array.Contains 方法检查 itemDescription 是否在单词数组中

string[] words = new String[] {null, null, null, null};
// as from John Saunders comment below, 
// pass everything into a local temp variable to avoid double 
// evaluation (and string rebuild) of the Trim() method
string key1 = txtComKeyword1.Text.Trim();
string key2 = txtComKeyword2.Text.Trim();
string key3 = txtComKeyword3.Text.Trim();
string key4 = txtComKeyword4.Text.Trim();
words[0] = (key1.Length == 0 ? null : key1.ToLower());
words[1] = (key2.Length == 0 ? null : key2.ToLower());
words[2] = (key3.Length == 0 ? null : key3.ToLower());
words[3] = (key4.Length == 0 ? null : key4.ToLower());
if (words.Contains(itemDescription.ToLower())) 
    .....

优点是双重的:

  • 您阅读文本框并在外面只转换为小写一次循环
  • if 语句大大简化

C# 中的三元运算符是表达常见编码模式的更简单方法

if(condition == true)
   variable = first statement;
else
   variable = second statement;

该代码变为三元运算符

   variable = (condition == true ? first statement : second statement);