0
votes

I am writing a C# forms application, that deals with a html document. I have a List of objects, where each object has an XPath for a html node.

When adding a html node, the XPath for these above objects changes.

My question is this: How is the best way to update all XPath values, after making an addition to a document? I am currently using Html Agility Pack.

EDIT

I have tried the following in code, but am not sure if Html Agility Pack has this feature, or I may be writing my code incorrectly:

  1. Before making any changes to the HtmlDocument, create a HtmlNode object for each XPath that I have
  2. Insert the new HtmlNode

I am creating the HtmlNodes with the following code:

HtmlNode htmlNode1 = testingHtmlDocument.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]");
  1. Should the XPath values in each of the HtmlNodes be updated after an addition has been made to the HtmlDocument? Does XPath work this way?
  2. Other than XPath, what is the best way to identify a HtmlNode, without adding any additional data to a HtmlNode?

Thanks.

1

1 Answers

0
votes

If you need to update the XPaths every time, you probably use indexing. And if you need to make changes often, maybe that is not the best way to go. Maybe you can add an ID to the tags? Then you could use: "//*[@id='0123-654-97854']"

  • ID's can be generated by using GUIDs
  • If you don't want to use the default id attribute for some reason you can also go for a custom data-myapp-id="..."
  • It may also be usefull to still specify the path to the tag (without indexes) like /Tag/Node/*[@data-myapp-id='...']