C# HtmlAgilityPack HtmlDocument:如何更新外部html?
我正在尝试使用HtmlAgilityPack更新外部Html。该属性显示为只读。我的问题是如何更新外部Html?注意:我确实需要更新外部html(不仅仅是内部html)。代码如下:C# HtmlAgilityPack HtmlDocument:如何更新外部html?,c#,html-agility-pack,xmldocument,C#,Html Agility Pack,Xmldocument,我正在尝试使用HtmlAgilityPack更新外部Html。该属性显示为只读。我的问题是如何更新外部Html?注意:我确实需要更新外部html(不仅仅是内部html)。代码如下: // Check if there is a nested table HtmlAgilityPack.HtmlNode nestedtable = tr.SelectSingleNode(".//table"); if (nestedtable != null) { // Save Inner/Outer
// Check if there is a nested table
HtmlAgilityPack.HtmlNode nestedtable = tr.SelectSingleNode(".//table");
if (nestedtable != null)
{
// Save Inner/Outer Html and update Outer Html
string strInnerHtml = nestedtable.InnerHtml;
string strOuterHtml = nestedtable.OuterHtml;
string strNewOuterHtml = "<table><tr><td><table><tr><td>inner1update</td><td>inner2update</td></tr></table></td></tr></table>";
// Now update source HtmlDocument
nestedtable.OuterHtml = strNewOuterHtml;
// ^^^ Error line: Property or indexer
//HtmlAgilityPack.HtmlNode.OuterHtml' cannot be assigned to -- it is read only
}
//检查是否存在嵌套表
HtmlAgilityPack.HtmlNode nestedtable=tr.SelectSingleNode(“.//表”);
如果(可嵌套!=null)
{
//保存内部/外部Html并更新外部Html
字符串strInnerHtml=nestedtable.InnerHtml;
字符串strOuterHtml=nestedtable.OuterHtml;
字符串strNewOuterHtml=“inner1updateinner2update”;
//现在更新源HtmlDocument
nestedtable.OuterHtml=strNewOuterHtml;
//^^^错误行:属性或索引器
//无法将“HtmlAlityPack.HtmlNode.OuterHtml”分配给--它是只读的
}
您可以在父级上使用ReplaceChild
,语法如下所示
var newNode = HtmlNode.CreateNode(strNewOuterHtml);
nestedtable.ParentNode.ReplaceChild(newNode, nestedtable);