下面的函数在这一行不能正常工作
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);
}
}
}