I am trying to programmatically get the XPath of an element of a webpage.
I am searching the HTML source for a specific pattern, when I find one, I get the returned string, and the start position of that string in the source.
What is the easiest and quickest way for me to then generate an XPath for that element based on its start position in the source?
Here's why asking for the XPath for a given part of an XML or HTML document is a mistake.
Character-based reference
If character-position indexing is truly ideal for your purpose, then treat your document as a string and access substrings via character positioning.  In this framework, there will be a single way to specify subparts of the document — the starting and ending character position.
Markup-based reference
If your purpose is to specify a part of a document based on the names and values of its elements (tags) and attributes, then it makes sense to use XPath:

Asking for the XPath isn't useful — there are many XPaths for any given document part.

A significant advantage of XPath is that it allows specification of a document part that can be invariant across different documents and changes to other parts of the document.


Markup such as XML and HTML associate higher-level structure with document parts beyond character positioning.  XPath leverages that reference framework and allows a higher-level specification of document parts.  Such references can be much more robust across both document instances and document changes.
Do not think in terms of the XPath but rather in terms of where in the space of element names, element values, attribute names, and attribute values the part of your document of interest resides.  The ability to specify document parts in those terms is the power XPath provides.
How to get the XPath for an element?