Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a doctype to HTML via HTML Agility pack

I know it is easy to add elements and attributes to HTML documents with the HTML agility pack. But how can I add a doctype (e.g. the HTML5 one) to an HtmlDocument with the html agility pack? Thank you

like image 444
alapeno Avatar asked Mar 31 '12 16:03

alapeno


2 Answers

As far as I know AgilityPack doesn't have a direct method to set the doctype, but as Hans mentioned, HAP treats the doctype as a comment node. So you could try to find the existing doctype first, if not create a new one and paste a desired value there:

var doctype = doc.DocumentNode.SelectSingleNode("/comment()[starts-with(.,'<!DOCTYPE')]");
if (doctype == null)
    doctype = doc.DocumentNode.PrependChild(doc.CreateComment());

doctype.InnerHtml = "<!DOCTYPE html>";
like image 190
Oleks Avatar answered Sep 25 '22 02:09

Oleks


The Html Agility Pack parser treats the doctype as a comment node. In order to add a doctype to an HTML document simply add a comment node with the desired doctype to the beginning of the document:

HtmlDocument htmlDoc = new HtmlDocument();

htmlDoc.Load("withoutdoctype.html");

HtmlCommentNode hcn = htmlDoc.CreateComment("<!DOCTYPE html>");

HtmlNode htmlNode = htmlDoc.DocumentNode.SelectSingleNode("/html");
htmlDoc.DocumentNode.InsertBefore(hcn, htmlNode);

htmlDoc.Save("withdoctype.html");

Please note, that my code does not check for the existing of a doctype.

like image 37
Hans Avatar answered Sep 25 '22 02:09

Hans