【问题标题】:HtmlAgilityPack HtmlDocument : How to update outer html?HtmlAgilityPack HtmlDocument:如何更新外部 html?
【发布时间】:2013-12-30 21:09:28
【问题描述】:

我正在尝试使用 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
}

【问题讨论】:

    标签: c# html-agility-pack xmldocument


    【解决方案1】:

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

    var newNode = HtmlNode.CreateNode(strNewOuterHtml);
    nestedtable.ParentNode.ReplaceChild(newNode, nestedtable);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多