This project has moved and is read-only. For the latest updates, please go here.

OptionWriteEmptyNodes break XML declaration

Mar 23, 2012 at 5:04 AM
Edited Mar 23, 2012 at 5:05 AM

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?

Apr 11, 2012 at 3:52 AM

anyone know how to resolve this issue?