How to Get element by class in HtmlAgilityPack

倾然丶 夕夏残阳落幕 提交于 2019-12-22 09:47:33

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/23040482/how-to-get-element-by-class-in-htmlagilitypack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!