2014-12-01 64 views
0

我需要搜索整个HTML文档并突出显示搜索到的关键字。我正在使用C#和XPath解决方案。我想我有种解决方案,但输出不是我想要的。使用XPath在整个HTML文档中查找并替换关键字

static string keyword = "red"; 

static void Main(string[] args) 
{ 
    string htmlString = @"<html> 
          <head> 
           <title>HTML sample page</title> 
          </head> 
          <body> 
           <div><div>This is inside div red paragraph</div></div> 
           <p>This is a red paragraph</p> 
           <p>This is a tes paragraph</p> 
           <p>This is a test paragraph</p> 
           <p>This is a paragraph red </p> 
          </body> 
          </html>"; 

    XmlDocument htmlDocument = new XmlDocument(); 
    htmlDocument.Load(new StringReader(htmlString)); 

    foreach (XmlNode node in htmlDocument.SelectNodes("//*[contains(., 'red')]")) 
    { 
     node.InnerText = node.InnerText.Replace(keyword, "highlight" + keyword + "highlight"); 
    } 
    Console.WriteLine(htmlDocument.InnerXml); 
} 

,我越来越看起来像输出:

<html>HTML sample pageThis is inside div highlightredhighlight paragraphThis is a highlightredhighlight paragraphThis is a tes paragraphThis is a test paragraphThis is a paragraph highlightredhighlight </html> 

输出似乎摆脱除了html标签的所有其他代码。 我做错了什么?

回答

0

你得到的第一个匹配是html标签,因为它包含红色!因此,您只需将其所有内容替换为带有额外突出显示字词的文字。

此外,如果您确实需要突出显示文本,则需要拆分文本并在其中插入红色(或类似的内容)节点。

相关问题