This project has moved and is read-only. For the latest updates, please go here.

Strange node search.

Topics: Developer Forum
Feb 20, 2014 at 8:08 PM
Edited Feb 20, 2014 at 8:20 PM
Hi there. There is some interesting behavior when searching nodes. I'll show it in example.
So the problem is when I have tr nodes collection, and goes through it using "foreach" I expect that //td[@class='name'] will select every name tag in current tr tag. But I always get only first name tag. So the result string will look like "Volcano Name,Volcano Name,Volcano Name,Volcano Name," Is this correct? Or I do not fully understand logic of //?

Using td[@class='name'] helps to collect exactly what is needed.
This is html, used for searching some names:
        <td class="name">Volcano Name</td>
        <td>Last Major Eruption</td>
        <td>Type of Eruption</td>
        <td class="name">Mt. Lassen</td>
        <td>Explosive Eruption</td>
        <td class="name">Mt. Hood</td>
        <td>Pyroclastic flows and Mudflows</td>
        <td class="name">Mt .St. Helens</td>
        <td>Explosive Eruption</td>
And here you have C# example:
class HtmlParser
        private HtmlDocument Document = new HtmlDocument();

        public HtmlParser(String file)

        public String Parse()
            var table = Document.DocumentNode.SelectSingleNode("//table");
            var trCollection = table.SelectNodes("tr");

            var result = String.Empty;
            foreach (var trNode in trCollection)
                var nameNode = trNode.SelectSingleNode("//td[@class='name']");
                var name = nameNode.InnerText;
                result += name + ",";
            return result;
Mar 6, 2014 at 9:30 PM
In Xpath // means go back to the root node.
In xpath .// means relative to the current node.

Does it work if you use this:
var nameNode = trNode.SelectSingleNode(".//td[@class='name']");