我如何才能找到并删除HTML头CSS参考? [英] How can I find and remove CSS references in HTML head?
问题描述
我创造的加入,缩小和COM preSS上的CMS系统CSS引用的服务。例如:
I have created a service to join, minify and compress css-references on a CMS system. Example:
在
<link href="/Files/css1.css" rel="stylesheet" type="text/css"/>
<link href="/Files/css2.css" rel="stylesheet" type="text/css"/>
<link href="/Files/css3.css" rel="stylesheet" type="text/css" media="all"/>
现在,你可以这样写:
<link href="/min.ashx?files=/Files/css1.css,/Files/css2.css,/Files/css3.css" rel="stylesheet" type="text/css" />
我的下一个任务是采取头部分的所有引用自动,由一个单一的线来替代它们,在这个例子中看到的。
My next task is to take all references in head section AUTOMATICALLY and replace them by one single line, as seen in the example.
我应该只替换那些具有落在这些规则:
I should only replace those that falls with in these rules:
- 在HREF开始/文件/,以避免试图加载外部外部
- 只有那些具有属性的媒体或与媒体=所有应该被包括在内,因为生成的CSS文件将只有一个设置。
我有存取权限的页面的原始HTML,而是卡在sucsfully定位的参考,不知道我是否应该为解析XML或使用正则表达式或如..
I have acces to the raw html of the page, but is stuck on sucsfully locating the references, not knowing if I should parse to xml or use regex or such..
任何人都可以点我在正确的方向?
can anyone point me in the right direction?
推荐答案
使用 HTML敏捷性包 。进攻粗略的计划:
Use HTML Agility Pack. Rough plan of attack:
-
HTML内容加载到的HTMLDocument对象。
Load the html content into an HtmlDocument object.
通过XPath的发现在的HTMLDocument对象的链接节点
Find the link nodes in the HtmlDocument object via XPath
VAR节点= doc.DocumentBody.SelectNodes(//头/链接[@类型=文本/ CSS]);
var nodes = doc.DocumentBody.SelectNodes("//head/link[@type='text/css']");
从这些节点中检索的HREF
Retrieve the hrefs from those nodes
字符串的href =节点[0] .Attributes [HREF]值;
string href = nodes[0].Attributes["href"].Value;
然后用新的节点替换节点。
Then replace the nodes with the new node.
这篇关于我如何才能找到并删除HTML头CSS参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!