How to Get element by class in HtmlAgilityPack

asp.net c# html-agility-pack

Question

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

Accepted Answer

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.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow