Normalize Space in HtmlAgilityPack


Here is the html snippet. How do I get normalize-space text ?

To search for the same fragment, but using Xpath //*[normalize-space()='Text1 Text2']



var htmlNodes = htmlDoc.DocumentNode.SelectNodes("*");

foreach (var node in htmlNodes)
   text += node.InnerText;

I get this string:

"\r\n                        \r\n                        \r\n                        \r\n                        \r\n                        Text1\r\n                    Text2"

Can I get a normal text ?

"Text1 Text2"

Popular Answer

You can use InnerText property instead:

var texts = document.DocumentNode.Descendants("div").Select(n => n.InnerText);

And combine them if you want:

var combined = string.Join(" ", texts);

To filter out empty values:

.Select(n => n.InnerText.Replace("\r\n", "")).Where(s => !string.IsNullOrEmpty(s));

Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why