2012-08-10 82 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

相關問題