User_4373
User_4373

Reputation: 297

Combine all the content from the multiple <p> tag inside a div, into a single string

I have the html content as:

<div class="editor-box">
   <div class="insert-ad">
     Some ad content
   </div>
   <p>paragraph 1</p>
   <p>paragraph2</p>
   <p>paragraph3</p>
   <div class="media ad-item">
        Another Ad Content
    </div>
   <p>Paragraph4</p>
   <p>Paragraph5/p>
   <p></p>
</div>

I wanted to merge all the text inside the <p> element into a single string at once.

My final OutputString as:

string Output = "paragraph 1 paragraph2 paragraph3 Paragraph4 Paragraph5"

I have tried:

var doc = await GetAsync(href);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div[@class='editor-box']/p"))
{
    string text = node.InnerText;
}

I have got the text from the individual <p> element, But Is there any way to select all the content from <p> with a single query so that the i do not need to loop all the node and merge into a another string object.

Upvotes: 1

Views: 935

Answers (2)

ravi kumar
ravi kumar

Reputation: 1620

For any reason if you don't want to manually loop over all the paragraph contents, you can always use LINQ and string.Join to achieve the same results.:

//1. Get the document
var doc = await GetAsync(href);

//2. Select all the paragraphs:
var paragraphNodes = doc.DocumentNode.SelectNodes("//div[@class='editor-box']/p");

//3. Select the content inside them:
var paragraphContentList = paragraphNodes.Select(node => node.InnerText);

//4. Join all the contents in a single string
var finalString = string.Join(" ", paragraphContentList);

//5. Done!
Console.WriteLine(finalString);

Remember to use the LINQ namespace using System.Linq;

Upvotes: 3

DarkRob
DarkRob

Reputation: 3833

You may try this... If you assign some id to your div element and also add runat=server.

System.IO.StringWriter sw = new System.IO.StringWriter();
       System.Web.UI.HtmlTextWriter htmltext = new System.Web.UI.HtmlTextWriter(sw);
                DivId.RenderControl(htmltext);
                string str = sw.GetStringBuilder().ToString();

Here DivId is the id assigned to the div.

Upvotes: 0

Related Questions