fetching span value from html document

.net c# html-agility-pack

Question

I have following xpath fetched using firefox xpath plugin

id('some_id')/x:ul/x:li[4]/x:span

using html agility pack I'm able to fetch id('some_id')/x:ul/x:li[4]

htmlDoc.DocumentNode.SelectNodes(@"//div[@id='some_id']/ul/li[4]").FirstOrDefault();

but I dont know how to get this span value.

update

 <div id="some_id">
    <ul>
      <li><li>
      <li><li>
      <li><li>
      <li>
         Some text
        <span>text I want to grab</span>
      </li>
    </ul>
    </div>

Accepted Answer

An alternative approach (without html-agility-pack) would be to use LINQ2XML. You can use the XDocument.Descendants method to take the span element and take it's value:

var xml = @" <div id=""some_id"">
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li>
     Some text
    <span>text I want to grab</span>
  </li>
</ul>
</div>";
var doc = XDocument.Parse(xml);
Console.WriteLine(doc.Root.Descendants("span").FirstOrDefault().Value);

The code can be extended to check if the div element has the matching id, using the XElement.Attribute property:

var doc = XDocument.Parse(xml);
Console.WriteLine(doc.Elements("div").Where (e => e.Attribute("id").Value == "some_id").Descendants("span").FirstOrDefault().Value);

One drawback of this solution is that the XML structure (HTML, XHTML) needs to be properly closed or else the parsing will fail.


Popular Answer

You don't need parse HTML with LINQ2XML, HTMLAgilityPack it's for it and it's more easy to obtain the node in the following way :

var html = @" <div id=""some_id"">
  <ul>
    <li></li>
    <li></li>
    <li></li>
    <li>
      Some text
      <span>text I want to grab</span>
    </li>
  </ul>
</div>";

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var value = doc.DocumentNode.SelectSingleNode("div[@id='some_id']/ul/li/span").InnerText;    
Console.WriteLine(value);



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why