The information I need for parsing is stored for example in this form.

<div class="rSide"> <tr class="temperature"> <td class="p1 ">+15°</td> <td class="p2 bR ">+14°</td> <td class="p3 ">+13°</td> <td class="p4 bR ">+16°</td> <td class="p5 ">+21°</td> <td class="p6 bR ">+23°</td> <td class="p7 cur">+21°</td> <td class="p8 ">+18°</td> </tr> </div> 

How to parse the information about the temperature, if the div with the rSide class is the only one in the document, while there are a lot of tags with the tr with the temperature class.

  • Is there no way to write xpath? or find this unique rside and check its descendants? - tym32167
  • @ tym32167 that's it, I do not know how to check its descendants. - Castiel_Luciefer2000
  • Well, can you find the node? Does the node have a Children property or something like that? - tym32167
  • This fragment is not valid, so you can not parse it. tr can only be inside the table - Andrey NOP
  • @ AndreiNOP Well, how can I, for example, refer to a descendant for example - Castiel_Luciefer2000

1 answer 1

Something like this:

 var parser = new HtmlParser(); var document = parser.Parse(@" <div class='rSide'> <table> <tr class='temperature'> <td class='p1 '>+15°</td> <td class='p2 bR '>+14°</td> <td class='p3 '>+13°</td> <td class='p4 bR '>+16°</td> <td class='p5 '>+21°</td> <td class='p6 bR '>+23°</td> <td class='p7 cur'>+21°</td> <td class='p8 '>+18°</td> </tr> <tr class='temperature'> <td class='p1 '>+16°</td> <td class='p2 bR '>+15°</td> <td class='p3 '>+14°</td> <td class='p4 bR '>+17°</td> <td class='p5 '>+22°</td> <td class='p6 bR '>+24°</td> <td class='p7 cur'>+22°</td> <td class='p8 '>+19°</td> </tr> </table> </div> "); var temperaturesElements = document.QuerySelectorAll("div.rSide table tr.temperature"); foreach (var element in temperaturesElements) { var p1 = element.QuerySelector("td.p1").Text(); var p2 = element.QuerySelector("td.p2").Text(); var p3 = element.QuerySelector("td.p3").Text(); var p4 = element.QuerySelector("td.p4").Text(); // $"p1={p1}".Dump(); // $"p2={p2}".Dump(); // $"p3={p3}".Dump(); // $"p4={p4}".Dump(); // "".Dump(); } 

Linqpad output:

enter image description here

Two comments.

  1. Probably, it would be nice for you to declare a class in which to hold p1 ... pN - but this is already yourself.
  2. @Andrey NOP has already said that the above html-code is not valid, I will add. I have somehow indicated when answering someone on the anglesharp tag that the invalid code is not recognized. You will debug long and hard - but you will never guess what the error is. I once killed a couple of hours on a completely trivial example.

And further. Do not be lazy, read the questions asked by the tag earlier on ru so - because anglesharp has exactly the same type of questions, you just need to understand the house selectors.

  • A long-asked question, but still relevant.) Thanks for the answer.) - Castiel_Luciefer2000