代码之家  ›  专栏  ›  技术社区  ›  DaveDev

如何以编程方式加载HTML文档以添加到文档的<head>?

  •  1
  • DaveDev  · 技术社区  · 14 年前

    我们从客户端获得了HTML“包装器”文件,我们需要将内容插入其中,然后呈现HTML。

    <head> 客户端包装器的一部分,例如对脚本文件、css和一些元标记的引用。

    所以我要做的是

    string html = File.ReadAllText(wrapperLocation, Encoding.GetEncoding("iso-8859-1"));
    

    现在我有了完整的HTML。然后在该字符串中搜索预定义的内容,并将我们的内容插入其中,然后呈现它。

    <

    我不想提及 System.Windows.Forms

    5 回复  |  直到 14 年前
        1
  •  1
  •   user243966 user243966    14 年前

    我自己也没试过这个图书馆,但这可能符合要求: http://htmlagilitypack.codeplex.com/

        2
  •  1
  •   Matt Urtnowski    11 年前

    你可以用 https://github.com/jamietre/CsQuery 编辑html dom。

    var dom = CQ.Create(html);
    var dom = CQ.CreateFromUrl("http://www.jquery.com");
    
    dom.Select("div > span")
    .Eq(1)
    .Text("Change the text content of the 2nd span child of each div");
    

    只需选择头部并添加到它。

        3
  •  0
  •   Mau    14 年前

    我使用 WebBrowser 控件,并通过其文档属性导航/更改文档。

        4
  •  0
  •   Peter    14 年前

    你在使用母版页吗?

    母版页有<asp:ContentPlaceHolder>'代表所有你想要的内容。

        5
  •  -1
  •   DaveDev    14 年前

    我找不到一个自动化的解决方案,所以它归结为一个黑客:

    public virtual void PopulateCssTag(string tags)
    {
        // tags is a pre-compsed string containing all the tags I need.
        this.Wrapper = this.Wrapper.Replace("</head>", tags + "</head>");
    }