我正在尝试使用Html Agility Pack在Html中添加新节点。
这是我的示例Html。
它将会变为:
这里是使用Html Agility Pack添加子元素的文档。
但是当我将它保存到文件时,出现了一个错误。
以下是错误信息:
这是我的示例Html。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>
</title>
<style type="text/css">
.csDBEB299A{text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt;line-height:14.65pt;mso-line-height-rule:exactly}
.cs15323895{color:#000000;background-color:transparent;font-family:Verdana;font-size:10pt;font-weight:normal;font-style:normal;}
</style>
</head>
<body>
<p class="csDBEB299A"><span class="cs15323895">This is a sample</span></p><p class="csDBEB299A"><span class="cs15323895"> </span></p><p class="csDBEB299A"><span class="cs15323895"> </span></p><p class="csDBEB299A"><span class="cs15323895">Table name: Table 1</span></p><p class="csDBEB299A"><span class="cs15323895"> </span></p><p class="csDBEB299A"><a name="_GoBack"></a><span class="cs15323895"> </span></p></body>
</html>
如果innerHtml以"Table name:"
开头,则应附加一个节点。
例如,我有这个:
<span class="cs15323895">Table name: Table 1</span>
它将会变为:
<span class="cs15323895">Table name: Table 1<h2>This is h2 heading</h2></span>
这里是使用Html Agility Pack添加子元素的文档。
https://html-agility-pack.net/append-child
这是我的代码:
var htmlDoc = new HtmlDocument();
htmlDoc.Load(htmlFile);
foreach (var item in htmlDoc.DocumentNode.Descendants())
{
if (!item.HasChildNodes)
{
var text = item.InnerHtml;
var textTosearch = "table name:";
if (text.ToLower().StartsWith(textTosearch))
{
HtmlNode h2Node = HtmlNode.CreateNode("<h2> This is h2 heading</h2>");
item.AppendChild(h2Node);
}
}
}
但是当我将它保存到文件时,出现了一个错误。
htmlDoc.Save(@"test.html");
以下是错误信息:
Process is terminated due to StackOverflowException.
An unhandled exception of type 'System.StackOverflowException'
我已经搜索了解决方案,但是其他问题中提出的解决方案在我的问题上不起作用。
我尝试了try和catch,但它没有给出发生错误的行。谢谢