tag using HtmlAgilityPack? Pretty strange! When I load and replace empty string using var document = new HtmlDocu...">

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?

+6
source share
4 answers
 document.OptionWriteEmptyNodes = true; 

Do the trick for you!

+9
source

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 /> 
+4
source
 StringWriter writer = new StringWriter(); var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings() { OmitXmlDeclaration = true }); document.OptionOutputAsXml = true; document.Save(xmlWriter); var newHtml = writer.ToString(); 
+2
source

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; 
+1
source

Source: https://habr.com/ru/post/952972/


All Articles