Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get element by class in HtmlAgilityPack

Hello i making HttpWebResponse and getting the HtmlPage with all data that i need for example table with date info that i need to save them to array list and save it to xml file

Example of html Page

<table>
<tr>
<td class="padding5 sorting_1">
<span class="DateHover">01.03.14</span>
</td>
<td class="padding5 sorting_1">
<span class="DateHover" >10.03.14</span>
</td>
</tr>
</table>

my code that not working i using the HtmlAgilityPack

 private static string GetDataByIClass(string HtmlIn, string ClassToGet)
    {
        HtmlAgilityPack.HtmlDocument DocToParse = new HtmlAgilityPack.HtmlDocument();
        DocToParse.LoadHtml(HtmlIn);
        HtmlAgilityPack.HtmlNode InputNode = DocToParse.GetElementbyId(ClassToGet);//here is the problem i dont have method DocToParse.GetElementbyClass
        if (InputNode != null)
        {
            if (InputNode.Attributes["value"].Value != null)
            {
                return InputNode.Attributes["value"].Value;
            }
        }

        return null;
    }

Sow i need to read this data to get the date 01.03.14 and 10.02.14 for be able to save this to array list (and then to xml file)

Sow any ideas how can i get this dates(01.03.14 and 10.02.14)?

like image 737
Vladimir Potapov Avatar asked Mar 20 '23 21:03

Vladimir Potapov


1 Answers

Html Agility Pack has XPATH support, so you can do something like this:

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//span[@class='" + ClassToGet + "']"))
{
    string value = node.InnerText;
    // etc...
}

This means: get all SPAN elements from the top of the document (first /), recursively (second /) that have a given CLASS attribute. Then for each element, get the inner text.

like image 140
Simon Mourier Avatar answered Mar 29 '23 17:03

Simon Mourier