如何让按类元素HtmlAgilityPack [英] How to Get element by class in HtmlAgilityPack

查看:111
本文介绍了如何让按类元素HtmlAgilityPack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我做HttpWebResponse和获取HtmlPage与我需要用最新信息示例表,我需要将它们保存到数组列表,并将其保存到XML文件中的所有数据

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

HTML页面的示例

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

我的code,使用的HtmlAgilityPack不工作我

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

播种我需要阅读此数据来获得日期14年3月1日和14年2月10日为能够拯救这个数组列表(然后到XML文件)

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)

母猪任何想法我如何能得到这个日期(14年3月1日和14年2月10日)?

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

推荐答案

HTML敏捷性包有XPath支持,所以你可以做这样的事情:

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

这意味着:您可以通过文档的顶部所有span元素(第一/),递归(秒/)有一个给定的类属性。然后,对于每一个元素,得到了内部文本。

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.

这篇关于如何让按类元素HtmlAgilityPack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆