Hi there I’ve just registered on this website, because I need some help.
I want to get results from the nyaa.eu website.
Basically:
- Table Node is called
<table class="tlist"> - Every row node is called
<tr class="tlistrow">also sometimes it’s ‘trusted tlistrow’ etc. - The Nodes I try to retrieve are:
<td class="tlistname"> <td class="tlistsize"> <td class="tlistsn"> and <td class="tlistln">
Firstly I’m retrieving a table which contains all the info about torrents:
HtmlNode hnTable = doc.DocumentNode.SelectSingleNode("//table[@class='tlist']");
So, next thing is retrieving all the rows which contains ‘tlistrow’ in its class attribute:
HtmlNodeCollection hncRows = hnTable.SelectNodes("//tr[contains(@class,'tlistrow')]");
And finally the problem is when I read every node it’s always the same one:
foreach (HtmlNode row in hncRows)
{
foreach (HtmlNode child in row.ChildNodes)
{
if (child.SelectSingleNode("//td[@class='tlistname']") != null)
{
MessageBox.Show("Something found!\n\n" + child.SelectSingleNode("//td[@class='tlistname']").InnerText);
break;
}
}
}
The text displayed in the messagebox is always the same, it looks like it only selects one node multiple times.
How can I fix this or if I am doing anything wrong, please correct me.
You need to understand the difference between relative XPath expressions and absolute XPath expressions.
A relative XPath expression is evaluated off (having as an initial context node) a specific node in the XML document.
An absolute XPath expression is evaluated against the whole XML document (having as initial context node the document-node).
Any XPath expression that starts with the character
/is an absolute XPath expression.Based on the provided code, you want to use a relative XPath expression with an initial context node, containd in the variable named
child.The problem is that the expression you are using:
starts with
/and is therefore an absolute XPath expression.This, passed to the
SelectSingleNode()method always selects the firsttdelement in the whole XML document, that has aclassattribute with string value"tlistname."Solution: Use a relative XPath expression, such as: