Hope somebody can help me.
Let´s say I have a html document that contains multiple divs like this example:
First of all, take a look at this: Html Agility Pack - Problem selecting subnode
Here is a full working solution for your question:
IList results = new List();
foreach (var node in doc.DocumentNode.SelectNodes("//div[@class='search_hit']")) {
var record = new Record();
record.Name = node.SelectSingleNode(".//span[@prop='name']").InnerText;
record.company = node.SelectSingleNode(".//span[@prop='company']").InnerText;
record.street = node.SelectSingleNode(".//span[@prop='street']").InnerText;
results.Add(record);
}
If you read the question I pointed you to, you will see that doing ./span[@prop='name'] is exactly the same, since those span nodes are (direct) children of the div node.
If the span nodes do not have those prop attributes, and you want to assign them depending on the order they appear, you can do:
foreach (var node in doc.DocumentNode.SelectNodes("//div[@class='search_hit']")) {
var spanNodes = node.SelectNodes("./span");
var record = new Record();
record.Name = spanNodes[0].InnerText;
record.company = spanNodes[1].InnerText;
record.street = spanNodes[2].InnerText;
results.Add(record);
}