How to replace tag with <br/"> tag using HtmlAgilityPack?
Pretty strange! When I load and replace empty string using
var document = new HtmlDocument(); document.LoadHtml(data); document.DocumentNode.OuterHtml.Replace("<tbody>", ""); This works fine and the <tbody> will be removed.
Same thing when I try to replace <br> with <br/> using
document.DocumentNode.OuterHtml.Replace("<br>", "<br/>"); Does not work: (
also tried
var brTags = document.DocumentNode.SelectNodes("//br"); if (brTags != null) { foreach (HtmlNode brTag in brTags) { brTag.OuterHtml = "<br/>"; // brTag.Name= "br/"; - > Also this one :( } } HTMLAgilityPack replace () function not working for self-closing tags?
You do not need to replace <br> with <br/> manually, if you need to close the node, just ask the library to do this, for example:
HtmlDocument doc = new HtmlDocument(); doc.LoadHtml("<br/>"); doc.Save(Console.Out); will output this:
<br> and this one
HtmlDocument doc = new HtmlDocument(); doc.LoadHtml("<br/>"); doc.OptionWriteEmptyNodes = true; doc.Save(Console.Out); will output this:
<br /> In fact, your first request should not work either, unless you assign the result of the replacement back to the document. Strings are immutable in C #. When you do Replace , a new line is created and returned. The original string remains unchanged.
OuterHtml is also read-only. You cannot assign it.
To remove nodes, you must select them, delete them, and save the result in the original row.
var document = new HtmlDocument(); document.LoadHtml(data); foreach (var tbody in document.DocumentNode.SelectNodes("//tbody")) tbody.Remove(); data = document.DocumentNode.OuterHtml; UPDATE:
foreach (var br in document.DocumentNode.SelectNodes("//br")) br.RemoveAllChildren(); HtmlNode.ElementsFlags["br"] = HtmlElementFlag.Closed | HtmlElementFlag.Empty; document.OptionWriteEmptyNodes = true; data = document.DocumentNode.OuterHtml;