vardos
vardos

Reputation: 334

HtmlAgility check if subclass with specific name exists

<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
   }
} 

Upvotes: 3

Views: 3903

Answers (1)

har07
har07

Reputation: 89285

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
} 

Upvotes: 5

Related Questions