简体   繁体   English

如何将 doctype 解析为字符串作为 html doctype

[英]How to parse doctype to string as html doctype

I'm working on C# app to get html file from current IE tab by EnumWindows .我正在使用 C# 应用程序从EnumWindows 的当前 IE 选项卡中获取 html 文件。 Now I got HTMLDocument and can parse it to html file from outerHTML ( {HTMLDocument}.documentElement.outerHTML ) by HtmlAgilityPack , but my output html file has not doctype.现在我得到了 HTMLDocument 并且可以通过HtmlAgilityPack 将其从 outerHTML ( {HTMLDocument}.documentElement.outerHTML ) 解析为 html 文件,但我的输出 html 文件没有 doctype。

I see that HTMLDocument has doctype property, how can I parse it to string as same as tag in html file我看到HTMLDocument具有doctype属性,如何将其解析为与 html 文件中的标签相同的字符串

I got it by casting htmlDocument.doctype as dynamic object.我通过将htmlDocument.doctype为动态对象来获得它。 Another, you can get other tags which are out of <html> tag by looping on (dynamic)htmlDocument.childNodes list另外,您可以通过循环(dynamic)htmlDocument.childNodes列表来获取<html>标签之外的其他标签

private static void InsertDocType(HTMLDocument htmlDocument, HtmlDocument document)
{
    // get html node
    HtmlNode htmlNode = document.DocumentNode.SelectSingleNode("/html");

    // get doctype node from HTMLDocument
    var doctype = (dynamic)htmlDocument.doctype;

    StringBuilder doctypeText = new StringBuilder();
    doctypeText.Append("<!DOCTYPE");
    doctypeText.Append(" ");
    doctypeText.Append(doctype.name);

    // add PUBLIC
    if (!string.IsNullOrEmpty(doctype.publicId))
    {
        doctypeText.Append(" PUBLIC \"");
        doctypeText.Append(doctype.publicId);
        doctypeText.Append("\"");
    }

    // add sytem id
    if (!string.IsNullOrEmpty(doctype.systemId))
    {
        doctypeText.Append(" \"");
        doctypeText.Append(doctype.systemId);
        doctypeText.Append("\"");
    }

    // add close tag
    doctypeText.Append(">");
    doctypeText.Append(Environment.NewLine);

    HtmlCommentNode doctypeNode = document.CreateComment(doctypeText.ToString());
    document.DocumentNode.InsertBefore(doctypeNode, htmlNode);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM