1

我正在搜索 XML 文件以查看是否有与这些文本框中插入的单词匹配的内容txtComKeyword1,txtComKeyword2txtComKeyword3/或txtComKeyword4. 下面的功能正在运行,但我可以知道如何突出显示用户在我的richComResultsrichtextbox 中出现的四个匹配的文本框中输入的关键字吗?

例如,我的用户将填写这四个文本框,即。txtComKeyword1、txtComKeyword2、txtComKeyword3 和 txtComKeyword4。然后,我的代码会解析XML文件,看看节点是否包含这四个关键字,如果是,节点的数据将输出到我的richComResults上,我想突出显示这四个关键字(eg 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");
                }
            }
    }
}
4

3 回答 3

3

尝试这个:

int pointer = 0;
int index = 0;
string keyword = "txtComKeyword1";

while (true)
{
    index = richComResults.Text.IndexOf(keyword, pointer);
    //if keyword not found
    if (index == -1)
    {
        break;
    }
    richComResults.Select(index, keyword.Length);
    richComResults.SelectionFont = new System.Drawing.Font(richComResults.Font, FontStyle.Bold);
    pointer = index + keyword.Length;
}

这将搜索关键字并突出显示它。然后在找到的关键字之后继续搜索。指针用于跟踪文本中的搜索位置。索引标记找到的关键字的位置。

于 2012-08-10T03:09:29.433 回答
0

试试这个代码:

void ParseLine(string line)
    {
        Regex r = new Regex("([ \\t{}():;])");
        String[] tokens = r.Split(line);


        foreach (string token in tokens)
        {
            // Set the tokens default color and font.
            richTextBox1.SelectionColor = Color.Black;
            richTextBox1.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);

            // Check whether the token is a keyword. 
            String[] keywords = { "Author", "Date", "Title", "Description", };
            for (int i = 0; i < keywords.Length; i++)
            {
                if (keywords[i] == token)
                {
                    // Apply alternative color and font to highlight keyword.
                    richTextBox1.SelectionColor = Color.Blue;
                    richTextBox1.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
                    break;
                }
            }
            richTextBox1.SelectedText = token;
        }
       richTextBox1.SelectedText = "\n";
    }

并用你的方法填充你的字符串 str 后调用我的方法:

string strRich =

            "Author : Habib\nDate : 2012-08-10 \nTitle : mytitle \nDescription : desc\n";


        Regex r = new Regex("\\n");
        String[] lines = r.Split(strRich);

        foreach (string l in lines)
        {
            ParseLine(l);
        }

请享用。

于 2012-08-10T03:13:13.473 回答
0

Jan 的回答包含了很多内容,但我在 while(true) 和 break aspect 中轻微地颤抖了一下!这是我的调整(不区分大小写)版本...

int nextHigh = RTF.Text.IndexOf(txSearch, 0, StringComparison.OrdinalIgnoreCase);
while (nextHigh >= 0)
{
    RTF.Select(nextHigh, txSearch.Length);
    RTF.SelectionColor = Color.Red;                            // Or whatever
    RTF.SelectionFont = new Font("Arial", 12, FontStyle.Bold); // you like
    nextHigh = RTF.Text.IndexOf(txSearch, nextHigh + txSearch.Length, StringComparison.OrdinalIgnoreCase);
}
于 2018-05-15T08:01:28.360 回答