I use HtmlAgilityPack to load HTML and write back, and I set OptionWriteEmptyNodes to true so as to keep the empty node, but if set OptionWriteEmptyNodes to ture, the XML declaration in origial HTML is also modified incorrectly.
Here is my code:
var doc = new HtmlDocument();
doc.OptionWriteEmptyNodes = true;
doc.LoadHtml(@"<?xml version=""1.0"" encoding=""utf-8"" ?> This is a test<br/>html");
var postParsed = doc.DocumentNode.WriteTo();
The output is:
<?xml version="1.0" encoding="utf-8" /> This is a test<br />html
You can see that the XML declaration <?xml version="1.0" encoding="utf-8" ?> is change incorrectly to <?xml version="1.0" encoding="utf-8" />
How to avoid this issue when set OptionWriteEmptyNodes to true?