Html Agility Pack: Setting an HtmlNode's Attribute Value isn't reflected in the HtmlDocument

霸气de小男生 提交于 2019-12-24 07:28:09

问题


In Html Agility Pack, when I set an attribute of an HtmlNode, should I see this in the HtmlDocument from which the node was selected?

Lets say that htmlDocument is an HtmlDocument. So the simplified code looks like this:

HtmlNode documentNode = htmlDocument.DocumentNode;
HtmlNodeCollection nodeCollection = documentNode.SelectNodes(someXPath);
foreach(var node in nodeCollection)
   if(SomeCondition(node))
       node.SetAttributeValue("class","something");

Now, I see the class attribte of node change, but I don't see this change reflected in the htmlDocument's html.


回答1:


Actually it was a case of ProgrammerTooStupidException :(

I used a MyHtmlPage class, with an Html property and an DocumentProperty.

_html = theHtml;
_htmlDocument = new HtmlDocument();
HtmlDocument.LoadHtml(theHtml)l
_documentNode = HtmlDocument.DocumentNode;

Now, of coourse manipulation the DocumentNode had no effect on the _html value.

Posting this reply to clear the name of HAP.



来源:https://stackoverflow.com/questions/4589959/html-agility-pack-setting-an-htmlnodes-attribute-value-isnt-reflected-in-the

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