outlook email
outlook email

Reputation: 361

Merge Node in HTML

How do I merge tag if the next consecutive tag is the same with the first tag?

I have this tag in my HTML

<p>This is a text<b>Focus on the Family Great Stories collection</b><b>—</b> This is a textagain <b>bold</b></p>

This should be the output

<p>This is a text<b>Focus on the Family Great Stories collection—</b> This is a textagain <b>bold</b></p>

And I have this code

What do I need to do with my code?

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);

            foreach (var item in doc.DocumentNode.Descendants("p").ToList())
            {
                if (item.HasChildNodes)
                {

                    foreach (var ch in item.ChildNodes)
                    {
                        if (ch.Name != "#text")
                        {
                            Console.WriteLine(ch.Name);
                        }
                    }
                }
            }

Upvotes: 0

Views: 143

Answers (1)

Hung Cao
Hung Cao

Reputation: 3208

This will give you an idea how to do this: You need to group by node name, and iterate through the result. After that, just append it to original doc.

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml("<p><b>Focus on the Family Great Stories collection</b><b>—</b></p>");

        foreach (var item in doc.DocumentNode.Descendants("p").ToList())
        {
            if (item.HasChildNodes)
            {
                var grouped = item.ChildNodes.GroupBy(_ => _.Name);
                HtmlNode newNode = doc.CreateElement(grouped.FirstOrDefault().Key);
                foreach (var bNode in grouped.FirstOrDefault())
                {
                    newNode.InnerHtml += bNode.InnerText;
                }
                item.InnerHtml = newNode.OuterHtml;
            }
        }

Upvotes: 1

Related Questions