Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HtmlAgilityPack HtmlDocument:如何更新外部html?_C#_Html Agility Pack_Xmldocument - Fatal编程技术网

C# HtmlAgilityPack HtmlDocument:如何更新外部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

我正在尝试使用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
}
//检查是否存在嵌套表
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);