C# HtmlAgilityPack inner html dont change after appending node

馋奶兔 提交于 2021-02-08 15:19:08

问题


In my C# i change loaded html, and need to get html document as plain text. But whenever i append new node to one of document's node, the inner html of root node doesn't change, even if the new node is successfully added. After debugging i noticed that only the parents of new node has the change in their InnerHtml property, for example:

HtmlDocument doc;
HtmlNode root doc.DocumentNode;
HtmlNode node2 = root.ChildNodes[1];
HtmlNode newNode = new HtmlNode(...);
node2.Append(newNode);

Having:

<root>
    <node1>
    </node1>
    <node2>
        <node3>
        <node3>
        <newNode>
        </newNode>
    </node2>
</root>

node2.InnerHtml will be

        <node3>
        <node3>
        <newNode>
        </newNode>

but root.InnerHtml is

<root>
    <node1>
    </node1>
    <node2>
        <node3>
        <node3>
    </node2>
</root>

How can i fix this right? ( i know i could manually update every document's node inner html, but common... )


回答1:


I have solved this issue by using method WriteContentTo() instead of using properties InnerHtml or OuterHtml



来源:https://stackoverflow.com/questions/20390901/c-sharp-htmlagilitypack-inner-html-dont-change-after-appending-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!