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

时间:2013-10-28 16:47:51

标签: c# asp.net

我有一个像这样的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;

2 个答案:

答案 0 :(得分:3)

使用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>

答案 1 :(得分:1)

string.Substring(string.IndexOf("<head>"), string.IndexOf("</head>") - string.IndexOf("<head>"));