Quantcast
Channel: htmlagilitypack Forum Rss Feed
Viewing all articles
Browse latest Browse all 655

New Post: Xpath in HtmlAgilityPack

$
0
0
The Html that I need to parse looks like this:
<tr><td class="listing1" ><b>Some text</b></td></tr>
                    <tr>stuff</tr>
                    <tr>stuff</tr>
                    <tr>stuff</tr>
                    <tr>stuff</tr>
                    <tr>stuff</tr>
 <tr><td class="listing1" ><b>Different text</b></td></tr>
                    <tr>stuff</tr>
                    <tr>stuff</tr>
                    <tr>stuff</tr>
                    <tr>stuff</tr>
                    <tr>stuff</tr>
I need to find the "tr" which contains a "td" with @class equals to "listing1". Then, I need to get all the following "tr" until it reaches the next "tr" which contains a "td" with @class equals to "listing1".

The first part I was able to do. I was able to find the "tr" which contains a "td" with @class equals to "listing1"

The following code do the first part and it also finds the first next "tr", but I need all the others "tr" between the "tr" which contains "td" with @class equals to "listing1".
public IEnumerable<DadosTitulo> ObterTitulos(string html)
        {
            document.LoadHtml(html);
            var trTipos = document.DocumentNode.SelectNodes("//tr[td[@class='listing1']]");
            foreach (var tr in trTipos)
            {
                //var proximos = tr.SelectNodes(".tr")
                var proximoTr = EncontrarProximoTr(tr);
                var tds = proximoTr.ChildNodes.Where(c => c.Name == "td").ToList();
                yield return new DadosTitulo
                {
                    Nome = tds[0].InnerText,
                    Vencimento = DateTime.Parse(tds[1].InnerText),
                    TaxaAnual = decimal.Parse(tds[2].InnerText.Replace("%", null)),
                    PrecoCompra = decimal.Parse(tds[4].InnerText.Replace("R$", null))
                };
            }

        }

        private HtmlNode EncontrarProximoTr(HtmlNode tr)
        {
            var proximo = tr.NextSibling;
            while (proximo != null)
            {
                if (proximo.Name == "tr")
                    return proximo;

                proximo = proximo.NextSibling;
            }

            return null;
        }
Need some help with the second part.

Viewing all articles
Browse latest Browse all 655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>