添加一个DOCTYPE HTML来通过HTML敏捷包 [英] Add a doctype to HTML via HTML Agility pack

查看:119
本文介绍了添加一个DOCTYPE HTML来通过HTML敏捷包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这很容易地添加元素和属性HTML与HTML敏捷包文件。但我怎么能添加一个文档类型(如HTML5的)到的的HTMLDocument 的使用HTML敏捷性包?谢谢

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

推荐答案

在HTML敏捷性包分析器把文档类型为注释节点。 为了一个文档类型添加到HTML文档,只要加入一

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");

请注意,我的code不检查一个文档类型的现有

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

这篇关于添加一个DOCTYPE HTML来通过HTML敏捷包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆