我需要搜索整个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标签的所有其他代码。 我做错了什么?