1
votes

I am parsing Web page using HTML agility pack where i traverse down to each required childnode
Here's how i am doing

HtmlDocument doc = web.Load("http://example.com");
HtmlNode list = doc.DocumentNode.SelectSingleNode("//ol[@class='list']")
foreach (HtmlNode inside_li in linode.ChildNodes)
{
  if (inside_li.Name == "div")
    {
       HtmlNode ahref = inside_li.SelectSingleNode("//h3[@desc='title']/a");
    }
}

Website Structure:- There are many ol with attrib class='list' each have a div which have h3 with atrrib desc='title' somewhere inside (location of h3 cannot be fixed).

Question:- Suppose if store ahref attrib ["href"] in array of 10 . then all elements have same String of first one. So i figured out using // it is referening the whole document http://example.com. for this case how can we search anywhere in inside_li HtmlNode. And why this problem is occurring even when I am telling to search inside of inside_li not whole document

1

1 Answers

2
votes

If you want to look only from the current node, just remove the // in an XPATH expression, for example:

inside_li.SelectSingleNode("h3[@desc='title']/a");

/ means "from the root"

// means "from the root" and "recursively"