0

下面的函数在这一行不能正常工作

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);
        }
    }
}
4

1 回答 1

1

在进入循环之前读取所有单词并将它们保存在字符串数组
中在将单词输入数组之前检查文本框是否为空
然后使用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);
于 2012-08-08T22:40:57.247 回答