Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetching span value from html document

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>
like image 278
user1765862 Avatar asked May 27 '26 16:05

user1765862


1 Answers

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);
like image 113
Vkt0r Avatar answered May 30 '26 04:05

Vkt0r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!