Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append a node to inner text using HTMLAgilityPack

Problem : I Need to remove the style property of all the <p> tags and if it contains font-weight:bold property, then add <b> to it.

eg : if the html is <p style="margin-top:0pt; margin-bottom:0pt;font-weight:bold; font-weight:bold;font-size:10pt; font-family:ARIAL" align="center"> SOME TEXT HERE</p>.

Output should be : <p align="center"> <b>SOME TEXT HERE</b></p>

I'm using the following Code,

var htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(htmlPage);
            foreach (var htmlTag in attributetags)
            {
                var Nodes = htmlDocument.DocumentNode.SelectNodes("//p");
                if (Nodes != null)
                {
                    bool flag = false;
                    foreach (var Node in Nodes)
                    {
                        if (Node.Attributes["style"] != null)
                        {
                            if (Node.Attributes["style"].Value.Contains("font-weight:bold"))
                            {                                    
                               var bnode = HtmlNode.CreateNode("<b>");
                               Node.PrependChild(bnode);
                            }
                            Node.Attributes.Remove("style");                            
                        }
                    }
                }
            }

I Have also tried with Node.InsertAfter(bcnode, Node), Node.InsertBefor(bnode, Node)

like image 781
user2729272 Avatar asked Sep 23 '13 06:09

user2729272


1 Answers

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
// select all paragraphs which have style with bold font weight
var paragraphs = doc.DocumentNode.SelectNodes("//p[contains(@style,'font-weight:bold')]");
foreach (var p in paragraphs)
{
    // remove bold font weight from style
    var style = Regex.Replace(p.Attributes["style"].Value, "font-weight:bold;?", "");        
    p.SetAttributeValue("style", style); // assign new style
    // wrap content of paragraph into b tag
    var b = HtmlNode.CreateNode("<b>");
    b.InnerHtml = p.InnerHtml;
    p.ChildNodes.Clear();
    p.AppendChild(b);
}

Wrapping content of paragraph can be done in one line, if you want:

p.InnerHtml = HtmlNode.CreateNode("<b>" + p.InnerHtml + "</b>").OuterHtml;
like image 178
Sergey Berezovskiy Avatar answered Oct 03 '22 03:10

Sergey Berezovskiy