user1765862
user1765862

Reputation: 14155

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>

Upvotes: 3

Views: 1459

Answers (2)

Victor Sigler
Victor Sigler

Reputation: 23449

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);

Upvotes: 2

keenthinker
keenthinker

Reputation: 7830

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.

Upvotes: 1

Related Questions