Reputation: 361
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
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