1

I am selecting a node with for example

HtmlDocument temp_HdDocument = new HtmlDocument();

temp_HdDocument.LoadHtml(mysource);

var vrTempNewNode = temp_HdDocument.SelectSingleNode("//a"));

I change node this way

foreach (HtmlNode node in vrTempNewNode.SelectNodes("*"))
{
    node.Remove();
}

Now when i make any changes on vrTempNewNode it is also reflected at temp_HdDocument

How can i prevent this happening ? I suppose select node just creates a reference object but i want it to be a new local variable not a part of main document

htmlagilitypack c# .net 4.5 wpf

3
  • Could you update your question with how you're changing it as well...
    – Prix
    Sep 6, 2014 at 12:51
  • "I suppose select node just creates a reference object..." Exactly right, you'll need to clone/copy the nodes to get independent entities. Sep 6, 2014 at 12:59
  • @Prix i added how i modify Sep 6, 2014 at 13:09

1 Answer 1

2

That's the default behavior, and it's very handy in most case. You can use HtmlNode.CopyFrom() to create an independent copy of the existing node that you can then modify without affecting the original HtmlDocument, for example :

var a = temp_HdDocument.SelectSingleNode("//a"));
HtmlNode temp = HtmlNode.CreateNode("<a></a>");
temp.CopyFrom(a, false);
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.