如何在HtmlAgilityPack中逐个获取元素
您好我制作HttpWebResponse并获取HtmlPage以及我需要的所有数据,例如带有日期信息的表,我需要将它们保存到数组列表并将其保存到xml文件
html页面示例
01.03.14 10.03.14
我使用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; }
母猪我需要读取此数据以获取日期01.03.14和10.02.14,以便能够将其保存到数组列表(然后再保存到xml文件)
播下任何想法我怎么能得到这个日期(01.03.14和10.02.14)?
Html Agility Pack支持XPATH,因此您可以执行以下操作:
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//span[@class='" + ClassToGet + "']")) { string value = node.InnerText; // etc... }
这意味着:从文档顶部(第一个/)获取具有给定CLASS属性的递归(第二个/)的所有SPAN元素。 然后为每个元素获取内部文本。