How Can I Find And Remove Css References In Html Head?
I have created a service to join, minify and compress css-references on a CMS system. Example: Before :
Solution 1:
Use HTML Agility Pack. Rough plan of attack:
Load the html content into an HtmlDocument object.
Find the link nodes in the HtmlDocument object via XPath
var nodes = doc.DocumentBody.SelectNodes("//head/link[@type='text/css']");
Retrieve the hrefs from those nodes
string href = nodes[0].Attributes["href"].Value;
Then replace the nodes with the new node.
Solution 2:
You can find the links that match your rules with regex:
<link href="(/Files/[^"]+)" .* media
It will give you the file path inside the quotes, e.g. '/Files/css1.css'. You can use that result to build up the string you wanted.
C# friendly regex:
@"<link href=""(/Files/[^""]+)"" .* media"
Use the Regex.Match method to get the groupings: http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx
Post a Comment for "How Can I Find And Remove Css References In Html Head?"