XPath to extract content?

c# dom html-agility-pack xml xpath


I have html content that I am storing as an XML document (using HTML Agility Pack). I know some XPath, but I am not able to zero into the exact content I need.

In my example below, I am trying to extract the "src" and "alt" text from the large image. This is my example:

   <div id="large_image_display">
      <img class="photo" src="images/KC0763_l.jpg" alt="Circles t-shirt - Navy" />
   <div id="small_image_display">
      <img class="photo" src="images/KC0763_s.jpg" alt="Circles t-shirt - Navy" />

What is the XPath to get "images/KC0763_l.jpg" and "Circles t-shirt - Navy"? This is how far I got but it is wrong. Mostly pseudo code at this point:


Any help in getting this right would be greatly appreciated.

10/16/2010 4:14:04 AM

Accepted Answer

The following xpath will get you to the src attributes for the img tags:


And similarly this will get you to the alt attributes:


From there you can get to the attribute text. If you want to only find the ones that match 'large_image_display' then you would filter it further like this:

10/16/2010 4:30:12 AM

Popular Answer

Use the following XPath expressions:




Always try to avoid using the // abbreviation, because it may result in very inefficient evaluation (causes the whole (sub)tree to be scanned).

In this particular case we know that the html element is the top element of the document and we can simply select it by /html -- not //html.

Your major problem was that in your expressions you were using \ and \\ and there are no such operators in XPath. The correct XPath operators you were trying to use are / and the // abbreviation.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow