HtmlAgility check if subclass with specific name exists

c# html-agility-pack

Question

<li class="sn-g">
     <span class="num">1</span>
     <span class="sym_first">
          <a class="icon>&nbsp;</a>
     </span>
     <span class="def">...text</span>
</li>

My HTML page contains such sub classes. However, the sym_first class is not present always. Using HTMLAgility I want to find whether sym_first class exists in the webpage. If it exists I want to get the InnerText from def class.

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//span[@class='" + sng + "']"))
{
   //How do I write this block?
   if(doc.DocumentNode.SelectNodes("//span[@class='" + symfirst + "']").Contains(xxx)
   {
      //get inner text
   }
} 

Popular Answer

You can try using SelectSingleNode() and check if the return value is not null :

if(doc.DocumentNode.SelectSingleNode("//span[@class='sym_first']") != null)
{
    //get inner text
}

or if you mean to check sym_first class within current li (assuming that you're looping through li in the code snippet in question) :

if(node.SelectSingleNode("span[@class='sym_first']") != null)
{
    //get inner text
}

UPDATE :

In response to the error reported in the comment below, try to check if def class exists as well :

var sym_first = node.SelectSingleNode("span[@class='sym_first']");
var def = node.SelectSingleNode("span[@class='def']");
if(sym_first != null && def != null)
{
    //get inner text
}

Depending on the requirements, you may want to iterate only through li elements that have these specific content in the first place :

var query = "//li[@class='sn-g'][span[@class='sym_first'] and span[@class='def']]";
foreach (HtmlNode node in doc.DocumentNode.SelectNodes(query))
{
   //get inner text
} 


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