我一直在使用 C#/ html 敏捷包中设置的节点工作一段时间,通过反复试验,我有一个要循环的节点列表,从循环中获取每个节点的子节点。我想通过索引 # 来引用它们(似乎是最简单的,但我在这里发布)。我尝试了不同的方法来格式化 xpath,包括“[0]”、“/[0]”、“tr/[0]”等。这是我到目前为止所拥有的,一切正常,直到第一个评论线:
protected override List<IDataPoint> ReturnDataPointsFromIndividualAddressString(string AddressString)
{
List<IDataPoint> earningsAnnouncements = new List<IDataPoint>();
HtmlWeb hwObject = new HtmlWeb();
HtmlDocument htmlDoc = hwObject.Load(AddressString);
if (htmlDoc.DocumentNode != null)
{
List<HtmlNode> nodeList = new List<HtmlNode>();
var nodes = htmlDoc.DocumentNode.SelectNodes("html[1]/body[1]/table[4]/tr[1]/td[1]/table[1]/tr");
if (nodes != null)
{
foreach (HtmlNode n in nodes)
{
if (n.OuterHtml.Contains("finance.yahoo.com"))
nodeList.Add(n);
}
}
foreach (HtmlNode node in nodeList)
{
EarningsAnnouncementDP earningsAnnouncement = new EarningsAnnouncementDP();
//Error: Expression must evaluate to a node set.
earningsAnnouncement.Company = (node.SelectSingleNode("[0]")).InnerText.ToString();
earningsAnnouncement.Ticker = node.SelectSingleNode("[1]").InnerText.ToString();
earningsAnnouncement.Estimate = node.SelectSingleNode("[2]").InnerText.ToString();
earningsAnnouncement.AnnouncementTime = node.SelectSingleNode("[3]").InnerText.ToString();
earningsAnnouncements.Add(earningsAnnouncement);
}
return earningsAnnouncements;
}