Normalize Space in HtmlAgilityPack

c# html-agility-pack xpath

Question

Here is the piece of HTML. How can I obtain text with normalized spacing?

To use Xpath to look for the same piece//*[normalize-space()='Text1 Text2']

<div>
    <div>
        <a></a>
        <a></a>
        <div><a><span></span>Text2</a></div>
    </div>
        <div>Text2</div>
</div>

Using:

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

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

This is what I get:

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

Can I receive a regular SMS message?

"Text1 Text2"
1
1
9/14/2018 6:54:44 PM

Popular Answer

You may utilizeInnerText instead of property

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

Add them together if you'd like:

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

Empty values should be filtered out:

.Select(n => n.InnerText.Replace("\r\n", "")).Where(s => !string.IsNullOrEmpty(s));
1
9/14/2018 6:43:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow