Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent HTMLAgilityPack from connecting words when using InnerText

I'm trying to do a simple task of getting text from HTML document. So I'm using HTMLdoc.DocumentNode.InnerText for that. The problem is that on some sites the don't put spaces between words when they are in a different tags. In those cases the DocumentNode.InnerText connect those word into one and it became useless.

for example, I'm trying to read a site contain that line

<span>İstanbul</span><ul><li><a href="i1.htm">Adana</a></li>

I'm getting "İstanbulAdana" which is meaningless.

I couldn't find any solution at HTMLAgilityPack documentation nor Google

Do I missing something?

Thanks,

like image 458
meirlo Avatar asked Aug 08 '12 08:08

meirlo


1 Answers

That should be rather easy to do.

const string html = @"<span>İstanbul</span><ul><li><a href=""i1.htm"">Adana</a></li>";
var doc = new HtmlDocument();
doc.LoadHtml(html);
string result = string.Join(" ", doc.DocumentNode.Descendants()
  .Where(n => !n.HasChildNodes && !string.IsNullOrWhiteSpace(n.InnerText))
  .Select(n => n.InnerText));
Console.WriteLine(result); // prints "İstanbul Adana"
like image 71
shriek Avatar answered Sep 18 '22 21:09

shriek