我正在搜索XML文件以查看是否存在与插入这些文本框txtComKeyword1
,txtComKeyword2
,txtComKeyword3
和/或txtComKeyword4
中的文字相匹配的内容。下面的功能正在工作,但是我可以知道如何突出显示用户在与我的richComResults
richtextbox中出现的匹配的四个文本框中输入的关键字?在RichTextBox中突出显示用户定义的关键字
例如,我的用户将填写这四个文本框,即。 txtComKeyword1,txtComKeyword2,txtComKeyword3和txtComKeyword4。然后,我的代码将解析XML文件,看看节点是否包含这四个关键字,如果是,节点的数据将在我的richComResults上输出,我想突出显示这四个关键字(例如txtComKeyword1 = hello,txtComKeyword2 = bye,txtComKeyword3 =早晨,txtComKeyword4 =夜晚)。这4个单词,如果找到并显示在richComResults中,将用颜色突出显示。
我在搜索一段时间后没有任何线索,我的情况与其他问题有很大不同。我是编程的新手,你的帮助将不胜感激。谢谢!
我的代码:
private void searchComByKeywords()
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
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 (txtComKeyword1.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword1.Text.ToLower()) ||
txtComKeyword2.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword2.Text.ToString()) ||
txtComKeyword3.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword3.Text.ToString()) ||
txtComKeyword4.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword4.Text.ToString()))
{
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");
}
}
}
}
但我想要的关键字不完全是“txtComKeyword1”。 :| txtComKeyword1是允许用户输入单词的文本框。 – Shyuan 2012-08-10 03:21:33
然后只需更改一行:'string keyword = txtComKeyword1.Text' – Jan 2012-08-10 03:45:47
好的,会试试看,并告诉你它是否有效。 :) – Shyuan 2012-08-10 03:57:31