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