0

i have an html file something like this :

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

<body>
// body contents ..
</body>
</html>

now i want to get header contents :

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

how to get this section?

something like :

string header = HTMLFile.header;
4

2 回答 2

3

Use HtmlAgilityPack to parse 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;

Result:

<css files="">
<js files="">
// maybe other things in header
</js></css>
于 2013-10-28T16:56:56.513 回答
1
string.Substring(string.IndexOf("<head>"), string.IndexOf("</head>") - string.IndexOf("<head>"));
于 2013-10-28T16:53:57.450 回答