如何以编程方式加载HTML文档以添加到文档中? [英] How to programmatically load a HTML document in order to add to the document's <head>?
问题描述
我们从客户端提供了HTML包装器"文件,我们需要将它们插入到内容中,然后呈现HTML.
We are supplied with HTML 'wrapper' files from the client, which we need to insert out content into, and then render the HTML.
在呈现插入了内容的HTML之前,我需要向客户端包装的< head>
部分添加一些标签,例如对脚本文件,css和某些文件的引用元标记.
Before we render the HTML with our content inserted, I need to add a few tags to the <head>
section of the client's wrapper, such as references to our script files, css and some meta tags.
所以我在做什么
string html = File.ReadAllText(wrapperLocation, Encoding.GetEncoding("iso-8859-1"));
,现在我有了完整的HTML.然后,我在该字符串中搜索预定义的内容,然后将我们的内容插入其中,然后进行渲染.
and now I have the complete HTML. I then search for a pre-defined content well in that string and insert our content into that, and render it.
如何创建HTML文档的实例并根据需要修改< head>
部分?
How can I create an instance of a HTML document and modify the <head>
section as required?
编辑:我不想引用 System.Windows.Forms
,因此WebBrowser不是一个选择.
edit: I don't want to reference System.Windows.Forms
so WebBrowser is not an option.
推荐答案
我对此没有自动化的解决方案,因此归结为黑客:
I couldn't get an automated solution to this, so it came down to a hack:
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>");
}
这篇关于如何以编程方式加载HTML文档以添加到文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!