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,
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With