0

I want to count the number of rows in html string returned from API. Any idea to get the rows count without using html agility pack ?

following code will connect to API and return html string for apiContent.

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var response = client.GetAsync(apiURL).Result;
    using (HttpContent content = response.Content)
    {
        Task<string> result = content.ReadAsStringAsync();
        apiContent = result.Result;
    }
}

now i need to count the numbers of row (tr) from html string in variable "apiContent" but without using html agility pack.

aloisdg
  • 22,270
  • 6
  • 85
  • 105
Yanly
  • 23
  • 2

2 Answers2

1

If the only <TR>'s being returned are what you are interested in, why not just do a LINQ .Count()?

int count = result.Count(f => f == '<tr');
Patrick
  • 5,526
  • 14
  • 64
  • 101
1

Here is a robust solution without HtmlAgilityPack.

Lets consider this HTML:

var html = "<table><tr><td>cell</td></tr><!--<tr><td>comment</td></tr>--></table>"

Lets load this HTML as a document:

// Create a new context for evaluating webpages with the default configuration
var context = BrowsingContext.New(Configuration.Default);
// Parse the document from the content of a response to a virtual request
var document = await context.OpenAsync(req => req.Content(html));

Query whatever you are looking for in your HTML:

var rows = document.QuerySelectorAll("tr");
Console.WriteLine(rows.Count());

Try it online!

Whenever you want to parse HTML, always rely on a HTML parser. If you dont want to use HAP, AngleSharp is a great alternative. If you dont want to use an existing HTML parser, you are doomed to make your own. It will be easier on a subset of HTML, but mostly not worth the hassle. Help yourself ; use a library.

aloisdg
  • 22,270
  • 6
  • 85
  • 105