Question

I have two code for getting no of characters inside templates first one is

string html = this.GetHTMLContent(url);

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);

StringBuilder sb = new StringBuilder();
foreach (HtmlTextNode node in doc.DocumentNode.SelectNodes("//text()"))
{
    sb.AppendLine(node.InnerText);
}
string final = sb.ToString();
int lenght = final.Length; 

And second one is

var length = doc.DocumentNode.SelectNodes("//text()")
                .Where(x => x.NodeType == HtmlNodeType.Text)
                .Select(x => x.InnerText.Length)
                .Sum();

When I run both code return me different result.

Was it helpful?

Solution

Finally I identified the problem. the problem was inside loop I used appendLine() method instead of append() method. so it appended new line each time of looping. So that some white spaces it also recognized as character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top