C#HtmlAgilityPack内部html附加节点后不更改 [英] C# HtmlAgilityPack inner html dont change after appending node
问题描述
在我的C#中,我更改了已加载的html,并且需要以纯文本格式获取html文档.但是,每当我将新节点追加到文档的节点之一时,即使成功添加了新节点,根节点的内部html也不会更改.调试后,我注意到只有新节点的父节点的InnerHtml属性有所更改,例如:
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);
具有:
<root>
<node1>
</node1>
<node2>
<node3>
<node3>
<newNode>
</newNode>
</node2>
</root>
node2.InnerHtml将是
node2.InnerHtml will be
<node3>
<node3>
<newNode>
</newNode>
但是root.InnerHtml是
but root.InnerHtml is
<root>
<node1>
</node1>
<node2>
<node3>
<node3>
</node2>
</root>
我该如何解决此问题?(我知道我可以手动更新每个文档的节点内部html,但很常见...)
How can i fix this right? ( i know i could manually update every document's node inner html, but common... )
推荐答案
我已经通过使用方法 WriteContentTo()
而不是使用属性 InnerHtml
或 OuterHtml
I have solved this issue by using method WriteContentTo()
instead of using properties InnerHtml
or OuterHtml
这篇关于C#HtmlAgilityPack内部html附加节点后不更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!