以下功能在此行无法正常工作
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);
}
}
}
答案 0 :(得分: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()))
.....
优点是双重的:
C#中的ternary operator是表达通用编码模式的更简单方法
if(condition == true)
variable = first statement;
else
variable = second statement;
该代码变为三元运算符
variable = (condition == true ? first statement : second statement);