HtmlAgilityPack HtmlDocument:如何更新外部html?

use*_*349 3 c# xmldocument html-agility-pack

我正在尝试使用HtmlAgilityPack更新外部Html.该属性显示为只读.我的问题是如何更新外部Html?注意:我确实需要更新外部html(不仅仅是内部html).这是代码:

// 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
}
Run Code Online (Sandbox Code Playgroud)

jes*_*ing 5

您可以ReplaceChild在父级上使用,语法如下所示

var newNode = HtmlNode.CreateNode(strNewOuterHtml);
nestedtable.ParentNode.ReplaceChild(newNode, nestedtable);
Run Code Online (Sandbox Code Playgroud)