Please help me with HTML agility pack/ xpath select child node by [index].

c#-4.0 html-agility-pack xpath


I've been working with a node set in C#/html agility pack for a while, and after some trial and error, I have a list of nodes that I want to loop through in order to retrieve the child nodes of each of the nodes that are in the loop. They should be referred to by their index #. (seems to be the easiest, yet here I post). I've experimented with several xpath formatting techniques, such as "[0]," "/[0]," "tr/[0]," etc. Here is what I currently have, with everything functioning OK up to the first commented line:

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(""))

                    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();


                    return earningsAnnouncements;
4/5/2012 9:10:09 PM

Accepted Answer

You've traveled up tilltr node. You should now accesstd node. in order to utilize theXPATH such as below.


also the firsttd accessible as nodetd[1] but nottd[0] .

As Alex pointed out, you could write something along these lines, which is a great idea.



4/6/2012 10:11:56 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow