2012-08-10 86 views
1

我正在搜索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"); 
       } 
      } 
    } 
} 

回答

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

这个搜索关键字,并强调它。然后它在找到的关键字之后继续搜索。指针用于跟踪文本中的搜索位置。该索引标记找到的关键字的位置。

+0

但我想要的关键字不完全是“txtComKeyword1”。 :| txtComKeyword1是允许用户输入单词的文本框。 – Shyuan 2012-08-10 03:21:33

+0

然后只需更改一行:'string keyword = txtComKeyword1.Text' – Jan 2012-08-10 03:45:47

+1

好的,会试试看,并告诉你它是否有效。 :) – Shyuan 2012-08-10 03:57:31

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

享受。

+0

它doesn没有真正反映我需要的东西:|我的用户将填写这四个文本框,即。'txtComKeyword1','txtComKeyword2','txtComKeyword3'和'txtComKeyword4'。然后,我的代码将解析XML文件以查看节点是否包含这四个关键字,如果是,节点数据将在我的'richComResults'输出,我想突出显示这四个关键字(例如,txtComKeyword1 = hello,txtComKeyword2 = bye, txtComKeyword3 =早晨,txtComKeyword4 =晚上)。这4个单词(如果找到并显示在richComResults中)将用颜色突出显示。 – Shyuan 2012-08-10 03:30:03

相关问题