读取其他文件的标头并将其复制到新文件中

本文关键字:文件 新文件 复制 其他 读取 | 更新日期: 2023-09-27 18:37:21

>我有一个像这样的HTML文件:

<html>
<head>
<css files>
<js files>
// maybe other things in header
</head>
<body>
// body contents ..
</body>
</html>

现在我想获取标题内容:

<css files>
<js files>
// maybe other things in header

如何获得此部分?

像这样:

string header = HTMLFile.header;

读取其他文件的标头并将其复制到新文件中

使用 HtmlAgilityPack 解析 html:

string html = File.ReadAllText("pathToFile");
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
HtmlNode head = doc.DocumentNode.SelectSingleNode("/html/head");
string headHtml = head.InnerHtml;

结果:

<css files="">
<js files="">
// maybe other things in header
</js></css>
string.Substring(string.IndexOf("<head>"), string.IndexOf("</head>") - string.IndexOf("<head>"));