Vladimir Potapov
Vladimir Potapov

Reputation: 2437

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

Upvotes: 4

Views: 20402

Answers (1)

Simon Mourier
Simon Mourier

Reputation: 139296

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.

Upvotes: 7

Related Questions